Restart Android Activity On Relaunch Of Application
Solution 1:
You can solve this problem by using following Way In your third Activity class put following Code,
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
startActivity(new Intent(getBaseContext(), YourSecondActivity.class));
finish();
}
Same way You can put in second Activity Class
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
startActivity(new Intent(getBaseContext(), YourFirstActivity.class));
finish();
}
Finally You can put this in your main class
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
finish();
}
Solution 2:
- Create three activities - A, B and C
In Activity A - when calling startActivity(B), call finish() also. Example -
public void onButtonClick() // Some method { startActivity(intentForB); finish(); }
Similarly when going to C from B -
public void onButtonClick() { startActivity(intentForC); finish(); }
When the user is on Activity C and when he presses the back button , the application will get closed.(No need to write back button handling explicitly).
Hope this helps.
Solution 3:
If you want to exit in all 3 Activities, then you must close the current Activity using finish()
while starting a new Activity.
Solution 4:
moveTaskToBack()
Move the task containing this activity to the back of the activity stack.
Do it in OnBackpressed()
and use finish()
in all your activities. (Because you said you want to finish all the activities when you have pressed back button)
publicvoidOnBackpressed()
{
finish();
}
and Simply, in your code do like this:
In activity 1
Intent intent=newIntent(activity1.this,activity2.class);
startActivity(intent);
finish();
In activity 2
Intent intent=newIntent(activity2.this,activity3.class);
startActivity(intent);
finish();
If you used this in your first two activities, Then in third activity no need to handle OnBackpressed()
. Because OnBackpressed()
is Called when the activity has detected the user's press of the back key. The default implementation simply finishes the current activity, but you can override this to do whatever you want.
Solution 5:
Use this code in your AndroidManifest.xml and android:clearTaskOnLaunch="true" in first launch activity.
<activityandroid:name="com.example.package.SplashActivity"android:label="@string/app_name"android:clearTaskOnLaunch="true"><intent-filter><actionandroid:name="android.intent.action.MAIN" /><categoryandroid:name="android.intent.category.LAUNCHER" /></intent-filter></activity>
If you want to use Key Event then use this code in your activity :
@OverridepublicbooleandispatchKeyEvent(KeyEvent event)
{
booleanresult=false;
switch(event.getKeyCode())
{
case KeyEvent.KEYCODE_BACK:
finish(); // or moveTaskToBack(true);
result = true;
break;
default:
result= super.dispatchKeyEvent(event);
break;
}
return result;
}
Post a Comment for "Restart Android Activity On Relaunch Of Application"