Reputation: 13
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
ActivityA
, then I start ActivityB
and show Invoice detail. ActivityB
, I start ActivityC
and user can select products into Invoice. 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?
Upvotes: 1
Views: 213
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
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
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
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