Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Skip going back to direct parent activity when pressed back

I have got a small problem in an Android app I am working on :

There are 3 activities namely A , B , C and the invocation is in the following order : A -> B -> C. While in C, when I press BACK button, it should go back to A[Instead of the B by default]. And pressing BACK in A will exit the program.

I tried to call an intent from C to A. But in this case the call invocation gets into a loop : A -> B -> C -> A since the new activity is pushed on top of the stack. As a result, when BACK is pressed at A, instead of exiting [A is the start], it goes to C and then B and then back to A in a needless circle.

It would be great if someone could give a better way to address this loopy scenario!

like image 344
aswin akhilesh Avatar asked Apr 18 '12 04:04

aswin akhilesh


People also ask

How do I skip back pressed activity on Android?

In Android we can change the default behaviour of the any activity like keypress, back button press, swap left swap right, to do Disable Back Press we need to override default functionality of onBackPressed() or onKeyDown() method in Activity class.

How pass data from second activity to first activity when pressed back Android?

Start Activity2 with startActivityForResult and use setResult method for sending data back from Activity2 to Activity1. In Activity1 you will need to override onActivityResult for updating TextView with EditText data from Activity2. If you can, also use SharedPreferences for sharing data between Activities.

What is intent FLAG_ activity_ new_ task?

launchMode — singleTask | flag — FLAG_ACTIVITY_NEW_TASK: If an Activity do not exist in an already created Task, then it starts the Activity in a new Task with Activity's new instance at the root of the Task's back stack, else the Task is brought forward with the Activity's last state restored and this Activity ...

What is back stack?

A task is a collection of activities that users interact with when trying to do something in your app. These activities are arranged in a stack—the back stack—in the order in which each activity is opened. For example, an email app might have one activity to show a list of new messages.


2 Answers

Very simple!! When you are starting the activity C, from B, use B.finish(). Something like this.

Intent i = new Intent(B.this, C.class);
B.this.finish();
startActivity(i);

This will remove B from the stack!

like image 101
nithinreddy Avatar answered Oct 10 '22 06:10

nithinreddy


Probably late, but for people who might find this in a search: You can add

        android:noHistory="true"

to your activity B in your AndroidManifest. This will also avoid that the onActivityResult() method is called in activity B when C returns a result though. I'ts basically like B disappears as soon as you start C.

like image 26
Jess Avatar answered Oct 10 '22 08:10

Jess