1

I have two Activity A and B.

  1. Launched Activity A.
  2. Started Activity A ---> Activity B
  3. On Backey pressed Activity B is destroying and navigating to A.

I do not want to destroy the Activity B, maintain in stack to reuse the same activity when i navigate from A to B again.

Actual: Activity B is loading URL in webview oncreate which creating every time and reloads the URL.

Expected: It should start Activity A to Activity B by not creating and should not reload again.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • 3
    So you want to "hide" the activity, instead of destroying it. Check this out - [onbackpressed to hide not destroy activity](https://stackoverflow.com/questions/5914040/onbackpressed-to-hide-not-destroy-activity) – Nero Jan 02 '19 at 11:25
  • Possible duplicate of [Sending Activity to background without finishing](https://stackoverflow.com/questions/2041891/sending-activity-to-background-without-finishing) – Manohar Jan 02 '19 at 11:27
  • when i use moveTaskToBack(true); it is taking Activity B to background. It doesn't help me. It should go to Activity A. When again i navigate to B, it should not recreate. – Rajesh Gaddam Jan 02 '19 at 11:29

3 Answers3

0
@Override
public void onBackPressed() {
  moveTaskToBack(true);
}

It'll move your activity to Stack and it'll remain there when you'll return. For more details look here

VVN
  • 1,607
  • 2
  • 16
  • 25
0

Try this solution

@Override
public void onBackPressed() {
this.startActivity(new Intent(YourActivity.this,ActivityA.class));  
}

Override the onBackPressed() in ActivityB and then launch the activity A

Quick learner
  • 10,632
  • 4
  • 45
  • 55
0

From Activity B, below code will not destroy the Activity B and navigate to A

@Override
public void onBackPressed() {
this.startActivity(new Intent(ActivityB.this,ActivityA.class));  
}

Navigating from Activity A to B, set below flag to intent which will not recreate the activity.

setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) 

Thanks.