Question

The question might be dumb but I'll ask it, for example I have 3 Activities, Activity 1,2,3.

Activity 1, I click a button, goes to Activity 2 Activity 2, I click a button, goes to Activity 3. Activity 3, I click the back button, I want it to go back to Activity 1. Any idea how to do it? Sorry Android Newbie. Thanks!

Was it helpful?

Solution

override onbackpressed method and just do like this:-

 @Override
 public void onBackPressed() {
        Intent intent = new Intent(Activity3.this,
                Activity1.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        startActivity(intent);

  }

Enjoy....!

OTHER TIPS

In the android.manifest file make your second Activity..

android:excludeFromRecents="true"
android:launchMode="singleTask"

Hope this does the trick. if this does not work then you can try by setting Intent.setFlags while you start the activity 2

Use SharedPref to store the 2nd prevActivity. Take to String pref (prevActivity_1 and prevActivity_2) to track your 1st and 2nd level previous activity. and update the pref when you are leaving the current activity. Now from any activity you can access the 2nd previous activity and call it.

Here is a Easy and Efficient way.. You can use forwarding to remove the previous activity from the activity stack while launching the next one. basically all you're doing is calling finish() immediately after calling startActivity().
My answer for you example is to call finish()immediately after calling startActivity() in activity b. and try backbutton after went activity c. Happy Coding..

ok when u click on 2nd activity start activity then clear 2 activity from stack and also write finish after call intent in 2nd activity and also see how to clear stack top :)

Try this : Back button start activity and Back button start activity

You can startActivity from onBackPressed() method.

Hope this helps.

Since Activity2 is pushed in the stack the back button of Activity3 will get you to Activity2. Hence the best you can do is not to push Activity2 in the stack. In your manifest.xml, where you define your activity do the following:

<activity android:name=".Activity2" android:noHistory="true" ... />

developer.android.com says:

A value of "true" means that the activity will not leave a historical trace. It will not remain in the activity stack for the task, so the user will not be able to return to it.

For more details: https://developer.android.com/guide/topics/manifest/activity-element.html

Hope this solves the issue :-) I know this question has been asked long time back, but it might help some...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top