John Fry
John Fry

Reputation: 13

Android - Activity navigation

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

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?

Upvotes: 1

Views: 213

Answers (4)

Chetan Kumar Patel
Chetan Kumar Patel

Reputation: 287

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

Upvotes: 0

Reaz Murshed
Reaz Murshed

Reputation: 24211

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.

Upvotes: 1

Vivek Bhardwaj
Vivek Bhardwaj

Reputation: 528

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..

Upvotes: 0

toffor
toffor

Reputation: 1299

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.

Upvotes: 0

Related Questions