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!
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.
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.
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 ...
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.
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!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With