1

enter image description here

I have problem with activity navigation. I have 3 activities.

1) ActivityA is list of Invoice

2) ActivityB is list of Items of Invoice (invoice detail)

3) ActivityC is product list

  • When user click on Invoice item in ActivityA, then I start ActivityB and show Invoice detail.
  • When user click on PLUS button in ActivityB, I start ActivityC and user can select products into Invoice.
  • When user click on BACK button in ActivityC, then ActivityC finishes and user is in ActivityB.

Ok, it is functional, but I need:

When user click on PLUS button in ActivityA, then create Invoice and directly start ActivityC for product selection. OK, I start ActivityC, user can select product. But problem is, when user click on Back button in ActivityC, I need to navigate back to ActivityB (to invoice detail) not to ActivityA. How can I do this?

What is the best solution for this problem?

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
John Fry
  • 13
  • 2

4 Answers4

1

You just have to override the onBackPressed function in your ActivityC to handle the transition among your activities. Here's how you do it.

While you are launching ActivityC from ActivityB, start the activity like this.

Intent intent = new Intent(this, ActivityC.class);
intent.putExtra("CallingActivity", "ActivityB");
startActivity(intent);

And while launching ActivityC from your ActivityA, you need to do the following.

Intent intent = new Intent(this, ActivityC.class);
intent.putExtra("CallingActivity", "ActivityA");
startActivity(intent);

Now in your ActivityC you need to check the value of the intent and save it somewhere. Then handle the onBackPressed function in your ActivityC.

String callingActivity = null;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_activity_c);
    Bundle bundle = getIntent().getExtras();
    callingActivity = bundle.getString("CallingActivity", null);
}

@Override
public void onBackPressed() {
    if(callingActivity != null && callingActivity.equals("ActivityB")) super.onBackPressed();
    else {
       Intent intent = new Intent(this, ActivityB.class);
       startActivity(intent); 
    }
}

Hope that helps.

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
0

In that case start Activity B from Activity C and finish Activity C.

void startActivityB(){
     Intent starter = new Intent(this, ActivityB.class);
     startActivity(starter);
     this.finish();
}

And ActivityA still will be in stack.

toffor
  • 1,219
  • 1
  • 12
  • 21
  • When I go A-->C-->B, it is ok, but when I go: A-->B-->C then back button in C create B and after back in B navigate again to B instead of A, because B is in Stack twice. – John Fry Mar 15 '18 at 10:12
  • this will prevent to create same activity twice. – toffor Mar 15 '18 at 10:27
0

Here is a solution that i think might work for you. On back press in Activity C .. fire intent for starting activity B .. this might help you..

Let me know if this worked for you! Else you need to show the code snippet..

Vivek Bhardwaj
  • 530
  • 5
  • 16
0

Override onBackpressed method in your activity c try this one

@Override
public void onBackPressed() {
   //code any action you want at here
   Intent activityB= new Intent(ActivityC.this, ActivityB.class);
   startActivity(activityB);finish();
}

Hope this will work for you