0

i have 3 activitys FirstActivity,SecondActivity,ThridActivity respectively, i put "intent" FirstActivity to SecondActivity , SecondActivity to ThridActivity.

Now if i click the backButton {onBackPressed()} on ThridActivity i want to go to the FirstActivity.

but i don't need 'Intent' to FirstActivity in 'onBackPressed()', i need two backpress code.

public void onBackPressed() {
    finish();
}

public void onBackPressed() {
    this.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK));
            this.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_BACK));
}

these codes are working only single backpress.

Rince Mathew
  • 27
  • 1
  • 6
  • `finish()` the second `Activity` when you start the third `Activity`. – Mike M. Jan 17 '19 at 18:10
  • You can achieve it in many ways: 1. Starting your activities with `startActvityForResult` and cascade close them https://stackoverflow.com/questions/10407159/how-to-manage-startactivityforresult-on-android. You can use different `launchMode`s defined in manifest https://medium.com/@iammert/android-launchmode-visualized-8843fc833dbe or use `LocalBoradcastReceiver`. – jczerski Jan 17 '19 at 18:59
  • 1
    Thanks, The first one works fine @MikeM. – Rince Mathew Jan 18 '19 at 05:35
  • Do you mean that you need double click functinality? to move from C->A without going to Actiivity B ? – Rohit Singh Jan 18 '19 at 06:05

2 Answers2

0

You may follow below flow for same:

  1. Start SecondActvity from FirstActivity. At the same time finish the SecondActivity. like this:

    startActivity(new Intent(FirstActivity.this,SecondActivity.class));

    finish();

  2. Now when you press back one time from ThirdActivity, directly navigate to the FirstActivity
Android Geek
  • 8,956
  • 2
  • 21
  • 35
0

You have to clear the instance of Second activity when you are going from second to third activity.

But don't clear the instance of FirstActivity. If u will clear the firstactivity instace by clicking back from third it will close completly.

Code :

On FirstActivity.

startActivity(new Intent(FirstActivity.this,SecondActivity.class));

On Second Activity.

startActivity(new Intent(SecondActivity.this,ThirdActivity.class));
finish();

On ThirdActivity.

public void onBackPressed() {
    finish();
}

public void onBackPressed() {
    this.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK));
            this.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_BACK));
}

Hope that helps you.If its working doesn't forget to make it as the correct answer.

Tapan Kumar Patro
  • 767
  • 1
  • 7
  • 18