using intents to start over the activity

2019-08-16 06:05发布

问题:

Intent i= new Intent( SetTest.this, SetTest.this);// compile error there
i.putExtra("question_number", questionNumber++);
startActivity(i);

I want to post the data to the same activity.. . Here is my intent filter in the mainfest file:

      <activity android:name="SetTest">
         <intent-filter android:label="@string/setQuestion">
                 <action android:name="android.intent.action.VIEW" />
                 <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

I dont understand why do I get the compile error?!?

I get compile error, with this too:

 Context context=getApplicationContext();

Intent i= new Intent(context, SetTest.this);

回答1:

AS the other answers say :

Intent i= new Intent( SetTest.this, SetTest.Class);
i.putExtra("question_number", questionNumber++);
startActivity(i);
finish();

As you're loading the same activity, you might want finish your current activity to avoid problem in your navigation tree



回答2:

Save off the context of the activity to a member varaible, and then use the saved context in your intent creation.

private Context _context;

Constructor...
{
_context = this;
}

Then in the button callback:

Intent i= new Intent( _context, newDesiredClass.class);


回答3:

use this

Intent i= new Intent( SetTest.this, SetTest.Class);

Hope this will work