1

For example, user passed between activities A to B. Now finish A and pass B to C, then finish B. Then click to the back button and go B again. Because B is the activity that the user comes from to current(ActivityC) activity.If the user go to the C from A directly, then finish A. If press the back button, then goes to the A again. Because A is the activity that the user comes from to current(ActivityC) activity. I want to set the onBackPress button to start activities that started before orderly. If I don't finish activities that started before when I start another activity, I can show the activity that started before with finish() current activity. This is possible. Because I don't finish any activity. So finish() current activity will cause show activity that started before. But, RAM will be overload when I open 50 activities may be. So because of this problem I need to finish() activity that started before. On the other hand, I want to start the activity that started before orderly when clicking to the back button. For example

  ActivityA --> ActivityB --> ActivityC --> ActivityD

In this scenario, I finish ActivityA, ActivityB, ActivityC orderly. But I want to start ActivityC back press of the ActivityD and I want to start ActivityB on the back press of ActivityC.

Lastly, I have a complex path in the application. And, I navigate to the ActivityD and I finished the ActivityF when my current activity is ActivityF

ActivityF --> ActivityD
ActivityF finished

Current activity:

ActivityD

In this scenario, I want to start the ActivityF not the ActivityC on back button of the ActivityD.

Normally:

ActivityA --> ActivityB --> ActivityC --> ActivityD /// click back button
ActivityA --> ActivityB --> ActivityC /// click back button
ActivityA --> ActivityB /// click back button
ActivityA

or

ActivityA --> ActivityF --> ActivityD /// click back button
ActivityA --> ActivityF /// click back button
ActivityA

But I want to generate this scenario:

ActivityA
--> ActivityB
--> ActivityC
--> ActivityD /// click back button
ActivityC <-- /// click back button
ActivityB <-- /// click back button
ActivityA 

or

ActivityA
--> ActivityF
--> ActivityD /// click back button
ActivityF <-- /// click back button
ActivityA <--
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
K.tas
  • 214
  • 1
  • 3
  • 16

3 Answers3

1

You should use Stack to keep track of your Activities. Stack in Android

Just push the current Activity before Intent to next Activity, and pop the current Activity in the onBackPressed() method.

You can save the Stack on SharedPreference.

EDIT:

This is the code. I used a DataHandler class to save the Stack.

DataHandler.java

public class DataHandler {
private static DataHandler mDataHandler;
private Deque<Intent> stack = new ArrayDeque<>();

private DataHandler() {
}

public static DataHandler getInstance(){
    if(mDataHandler == null){
        mDataHandler = new DataHandler();
    }
    return mDataHandler;
}

public Deque<Intent> getStack() {
    return stack;
}

public void setStack(Deque<Intent> stack) {
    this.stack = stack;
}
}

M1Activity.java

public class M1Activity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_m1);
    Button btn = findViewById(R.id.btn);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(M1Activity.this,M2Activity.class);
            startActivity(intent);
            DataHandler.getInstance().getStack().push(intent);
            finish();
        }
    });
}

@Override
public void onBackPressed() {
    super.onBackPressed();
    if (DataHandler.getInstance().getStack().size() > 0){
        Intent intent = DataHandler.getInstance().getStack().pop();
        startActivity(intent);
    }
}
}

Just like M1Activity, you have to push and pop intent from Stack in every Activity.

Rajat Mehra
  • 1,462
  • 14
  • 19
0

You can use

 if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {           
   finish();
 } else {
   ActivityCompat.finishAffinity(RatingReviewActivity.this);
 }
ppreetikaa
  • 1,149
  • 2
  • 15
  • 22
0

You will need to pass ActivityA (Parent Activity) name to ActivityB (Child Activity) to keep reference of parent activity and use it in onBackPressed method need for starting parent activity.

When starting activity:

private void startActivity(){
    Intent intent =new Intent(this, ActivityB.class);
    intent.putExtra("parentClassName",this.getClass().getName());
    startActivity(intent);
    finish();
}

In child activity :

public class ActivityB extends AppCompatActivity {

    String parentClassName;

    //..

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        parentClassName = getIntent().getExtras().get("parentClassName").toString();

        //..
    }

    @Override
    public void onBackPressed() {
        try {
            Intent intent = new Intent(this, Class.forName(parentClassName));
            startActivity(intent);
        } catch (ClassNotFoundException e) {

        }

        finish();
    }
}
ahsanali274
  • 471
  • 2
  • 14
  • Thank you for your answer. This is useful but If there is more than one tail. For example, A to B, B to C, C to D. And with your code I can only access Activity C. How can I go back more than one by this way – K.tas May 31 '19 at 12:14
  • You will need to use this method in every activity. That way each activity will keep track of it parent activity. – ahsanali274 May 31 '19 at 12:19
  • Okay. But this is available only passing between two activity, not more. I cannot go back two step correctly. The second step will go back to again last activity. Because I am getting parents name. For example, A to B, B to C, C to D and one onBackPressed take user to C and send onBackPressed will be clicked from C activity and it will start activity D not B. Because we take D's name – K.tas May 31 '19 at 13:16