2

I want to accomplish this structure:

Activity A is showing. If some button is pressed then open activity B (without closing current instance of A). If I press a back button of B I want just to finish B so that I can see my old instance of A. But if in activity B I press another button, I want to close A and open C.

How can I close activity A and start activity C when activity B is opened?

Explanation: When B is active the A mustn't be destroyed so that I could return to it. But if I want to open C then A must be destroyed, so that if I'd press back button of C I wouldn't see it anymore.

I already implemented the code that opens all of the activities by using startActivity() and finish() methods. All I need right now is an answer or suggetion of how could I rework my structure to accomplish my goal.

EDIT

I think I've got an idea to use startActivtyForResult() when I want to open B, so that when I'm ready to open C I'd just let A do this with closing itself.

GV_FiQst
  • 1,487
  • 14
  • 30
  • 2
    Why do you want to keep A open? What are you going to do about the OS randomly deciding to close A on its own while you are in B? – OneCricketeer Dec 17 '16 at 02:00
  • You should be implementing some logic in onPause() to persist any data required when returning to an active state in case the Activity is destroyed by the framework. – Mark Dec 17 '16 at 02:06
  • @cricket_007 It's all about animation. When I press back button I want to see B closing instead of opening A. Yes I could do it like startActivity(new Intent(this, A.class)); finish();. But if I do so I would see A openning. And also I want to keep data from A without passing them to B and then again passing them back to A. – GV_FiQst Dec 17 '16 at 02:08
  • You can override the transition – OneCricketeer Dec 17 '16 at 02:11
  • Or you could probably use Fragments instead. – OneCricketeer Dec 17 '16 at 02:11
  • @cricket_007 How can I do this? – GV_FiQst Dec 17 '16 at 02:12
  • 1
    Use different launch flags with starting your Activity - there is a whole bunch of them. https://developer.android.com/reference/android/content/Intent.html – Mark Dec 17 '16 at 02:14
  • If you mean Fragments, then somewhere in the docs, there any many pages on "effective navigation" and "fragment reuse" or "master-detail flow". https://developer.android.com/training/basics/fragments/index.html – OneCricketeer Dec 17 '16 at 02:17
  • @cricket_007 no I meant transition. Fragments cannot be used in this particular case. – GV_FiQst Dec 17 '16 at 02:19
  • Here is the question I am referring to. I can't remember the code for "show no animation". http://stackoverflow.com/questions/3515264/can-i-change-the-android-startactivity-transition-animation You can poke around in the `android.R.anim` values to see if there are any that seem to mean "nothing" – OneCricketeer Dec 17 '16 at 02:30

3 Answers3

6

When you Press Button C go to the ActivityC you just need to pass addFlag method with intent as follows

public void onClick(View v) {
        if(v.getId()==R.id.butoonC){
                Intent intent = new Intent(this, ActivityC.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);  
  }
}

here Intent.FLAG_ACTIVITY_CLEAR_TOP will remove all the activity from activity stack except activity B and activity C. So when u backpress from Activity B you Activity will not able to go back to Activity A. I hope this work for u

Saurabh Bhandari
  • 2,438
  • 4
  • 26
  • 33
  • It didn't work. When I pressed back button on C, A activity was shown. – GV_FiQst Dec 17 '16 at 09:46
  • if you are going from A to C then you press back press it will not work . if you go as per your question A to B and B to C.it will work for u . – Saurabh Bhandari Dec 17 '16 at 09:54
  • I did as I said before. In Activity B I wrote this code to open C and when it was opened I pressed back button at C activity. My expected result was to see app closing but actual result was activity A. Look below I found a solution for this problem myself. – GV_FiQst Dec 17 '16 at 10:00
0

Here is how I've solved the problem:

Activity A:

//Start Activity B 
startActivityForResult(new Intent(this, B.class), 1);

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        startActivity(new Intent(this, C.class));
        finish();
    }
}

Activity B:

//back button press:
setResult(RESULT_CANCELED, new Intent());
finish();

//start Activity C button:
setResult(RESULT_OK, new Intent());
finish();

Hope it will help someone.

GV_FiQst
  • 1,487
  • 14
  • 30
0

I use this and worked for me

in Activity A when a button press to go to Activity B use this code :

button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
               Intent intent = new Intent(ActivityA.this, ActivityB.class);
               startActivity(intent);
               ActivityA.this.finish();
        }
    });

It is close Activity A and when back button pressed do not come back to this Activity.

Ali Khaki
  • 1,184
  • 1
  • 13
  • 24
  • Thanks for effort but your answer is not applicable for the question. Please double check what exactly was the question about and look at the accepted answer. Take care! – GV_FiQst Sep 17 '18 at 11:36