0

In my android app I am stacking different instances of the same activity over top of each other (it is a requirement and I cannot change that).

I also have a close button on my activity's toolbar where the current functionality is that if user clicks on the close button it will close the current instance of the activity. Back button also have the same functionality.

But I want to change this behaviour. The back button should work the same way it is working now (finish the current instance) but the close button should finish all the instances of that activity and return to the parent activity where the transition started.

I cannot use the intent flags like Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOPbecause then the back button will also close all the instances of the activity.

I need something on theonOptionsItemSelected which close the activity and return to the parent activity. Please provide any hint if you can. Thanks

Ezio
  • 2,837
  • 2
  • 29
  • 45
  • You mean you want to clear all activity instances from memory and just want to go Parent(Home) activity, right? – Nitin Patel May 15 '17 at 06:36
  • I think you should use fragment which is better option or you have to store all values of parent activity and start that activity again with those saved values. – Nitin Patel May 15 '17 at 06:40
  • @NitinPatel yes sir, as I mentioned in the question, I have to create multiple instances of that activity that is a business requirement we perform some steps and everytime load the activity with different data – Ezio May 15 '17 at 06:42
  • 1
    AFAIK, For single activity that's not possible but I have said in previous , you can store all values of parent activity and start that activity again with those saved values that is the possible solution. – Nitin Patel May 15 '17 at 06:45
  • I am actually doing that. killing all activities and launching the parent activity afresh and it working fine. But since I am launching the activity again the previous state is lost, meaning the user is again at the start of the activity screen and not where he left. Any idea how I can avoid this and return to the previous state of the activity. – Ezio May 15 '17 at 07:00
  • 1
    I think that this link will help to your problem:- http://stackoverflow.com/a/15933890/7806873 – Nitin Patel May 15 '17 at 07:06
  • It worked man. Please add an answer so that I can accept it – Ezio May 15 '17 at 07:13
  • Just upvote my comment as useful. That will be best reward. – Nitin Patel May 15 '17 at 07:14

1 Answers1

0

You can use different action on both clicks

 @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
            case android.R.id.home:
                onBackPressed();
                break;
            case YOUR_ID_IN_MENU:
                onCloseClick();
                break;
        }

        return super.onOptionsItemSelected(item);
    }
Farya
  • 277
  • 3
  • 14
KKSINGLA
  • 1,284
  • 2
  • 10
  • 22