I have 3 intents
A->B->C
from C to get back to A you bring up the menu and click home. This finishes B and C and opens A which in the manifest is set as a singletask.
This all works perfectly, but when I try to open B from A again I have to click twice on the button that starts B. Whereas when the app first opens I have to click only the once to open B
Why could this be like this?
I think I know why. I think B is not finishing when I go from C to A. This is the code running on C
Intent Intent = new Intent(this, com.home.test.Home.class);
this.setResult(1, Intent);
startActivity(Intent);
this.finish();
And it should trigger this on B if I am correct
public void onActivityResult(int requestCode, int resultCode, Intent data) {
this.finish();
}
You do not need singleTask, in all likelihood. In C, when you call startActivity(), pass FLAG_ACTIVITY_CLEAR_TOP|FLAG_ACTIVITY_SINGLE_TOP as the Intent flags. That will finish C and B en route to starting A.
Related
I have android activity flow somewhat like
A->B->A
Moving from A to B and from B to A using intent and finish Activity B. On when I click on backpress I again go to activity A instead of exit from application.
I tried to set Intent Flags but was not able to achieve it.
Here is code to move from Activity A -> Activity B
Intent intent = new Intent(A.this, B.class);
startActivity(intent);
If here I backpress than I need to go to activity A again
From Activity B -> Activity A
Intent intent = new Intent(B.this, A.class);
startActivity(intent);
finish();
But when I backpress from here I need to exit the application
To go from B to A you don't need to do anything. If the user presses the BACK key, the default implementation calls finish() on B which will return the user to A.
If you have provided a button or something in B to go back to A, just call finish().
I want to accomplish this structure:
Activity A is showing. If some button is pressed then open activity B (without closing current instance of A). If I press a back button of B I want just to finish B so that I can see my old instance of A. But if in activity B I press another button, I want to close A and open C.
How can I close activity A and start activity C when activity B is opened?
Explanation: When B is active the A mustn't be destroyed so that I could return to it. But if I want to open C then A must be destroyed, so that if I'd press back button of C I wouldn't see it anymore.
I already implemented the code that opens all of the activities by using startActivity() and finish() methods. All I need right now is an answer or suggetion of how could I rework my structure to accomplish my goal.
EDIT
I think I've got an idea to use startActivtyForResult() when I want to open B, so that when I'm ready to open C I'd just let A do this with closing itself.
When you Press Button C go to the ActivityC you just need to pass addFlag method with intent as follows
public void onClick(View v) {
if(v.getId()==R.id.butoonC){
Intent intent = new Intent(this, ActivityC.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
}
here Intent.FLAG_ACTIVITY_CLEAR_TOP will remove all the activity from activity stack except activity B and activity C. So when u backpress from Activity B you Activity will not able to go back to Activity A.
I hope this work for u
Here is how I've solved the problem:
Activity A:
//Start Activity B
startActivityForResult(new Intent(this, B.class), 1);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
startActivity(new Intent(this, C.class));
finish();
}
}
Activity B:
//back button press:
setResult(RESULT_CANCELED, new Intent());
finish();
//start Activity C button:
setResult(RESULT_OK, new Intent());
finish();
Hope it will help someone.
I use this and worked for me
in Activity A when a button press to go to Activity B use this code :
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(ActivityA.this, ActivityB.class);
startActivity(intent);
ActivityA.this.finish();
}
});
It is close Activity A and when back button pressed do not come back to this Activity.
I have two activities, one which is transparent with a second activity showing underneath it. Is it possible to tap the transparent activity, dismiss it and have the tap carry through to the activity underneath?
Not sure what you mean exactly by "carry through", but this is what you can do in Android.
Start Activity A. When starting Activity B, start as such:
Intent intent = new Intent(A, B);
startActivityForResult(intent, SOME_RESULT_CODE);
Then when you dismiss Activity B, aka call finish(), you need to make sure you send a result:
Intent goBackToActivityA = new Intent();
goBackToActivityA.putExtra(PUT_EXTRA_FOR_WHATEVER_YOU_WANT_IN_A);
setResult(Activity.RESULT_OK, goBackToActivityA);
finish();
And then when you get back to Activity A, you will need to capture that result and handle it:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == SOME_RESULT_CODE) {
if(resultCode == Activity.RESULT_OK){
// extract PUT_EXTRA_FOR_WHATEVER_YOU_WANT_IN_A here
// whatever the "carry through" is you can manage here
}
}
}
I have three activities (a, b, c).
Activity a
- has one button for activity b and one button to close the app (by calling onfinish()).
- is the main activity, which runs b.
Activity b
- is the middle one which runs c.
After clicking on a back button that I placed in the header of activity c, and then clicking on the button of the a activity to close the app, I am returned to activity c. This is not the behavior I expected, any ideas about what is happening?
Further information:
activity c has onResume() because I also have an activity d. so when I click back button of activity d, it returns to activity c.
so long as I don't run the activity c, the close button works as expected.
try this.
Intent intent = new Intent(getApplicationContext() or UrActivity.this, FirstActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
For going previous activity just call
finish() ;
at back button of any activity .don't do this
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
and from exiting the application use
Intent intent = new Intent(getApplicationContext(), a.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
So I have 2 activities lets say A and B. A navigates to B, I want the activity A to be killed or make it unusable/unseen when directed from B.
so it should be like when I press the back button on B activity it should not open activity A instead it should go to the app tray.
also activity A should comeback when I clear the application data
thanks.
You could do this in one of two ways. First is finishing ActivityA so that it can't be resumed to later. When starting ActivityB from ActivityA, you'd do something like this:
Intent intent = new Intent(this, ActivityB.class);
startActivity(intent);
this.finish();
Another way is to just finish ActivityA when it gets a result of any sort from ActivityB. This code would also be in ActivityA.
To start ActivityB:
Intent intent = new Intent(this, ActivityB.class);
startActivityForResult(intent, REQUEST_ACTIVITYB);
To make sure ActivityA doesn't resume:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == REQUEST_ACTIVITYB) {
finish();
}
}
REQUEST_ACTIVITYB is just an int of your choosing.