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().
Related
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 3 Activities:
A: Parent Activity
B: first Child Activity
C: second Child Activity
In A Activity open B Activity when button is clicked
Intent i = new Intent(this, ActivityB.class);
startActivityForResult(i, 1);
In A Activity there is a Runnable object that in some condition open C Activity
Intent i = new Intent(this, Activityc.class);
startActivityForResult(i, 2);
All work and on device i see C Activity over B Activity over A Activity but when i close C Activity with this.finish(); in A Activity onActivityResult() event is not fired. It'll be fired only if i close the B Activity too.
How can i know in A Activity when C Activity is closed and let B Activity opened?
Whenever you wish to exit all open activities, you should press a button which loads the first Activity that runs when your application starts then clear all the other activities, then have the last remaining activity finish. to do so apply the following code in ur project
Intent intent = new Intent(getApplicationContext(), FirstActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("EXIT", true);
startActivity(intent);
The above code finishes all the activities except for FirstActivity. Then we need to finish the FirstActivity's Enter the below code in Firstactivity's oncreate
if (getIntent().getBooleanExtra("EXIT", false)) {
finish();
}
Please See this
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);
I have five activity for example
A, B, C, D, E
I am going from A -> B -> C -> D -> E
now when i press button from E
i want to go to directly B and B should not create new actiivity
I know that
Intent intent = new Intent(this, ActivityB.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
by this i can do it but i don't want to create activity B by doing startActivity
In the button's onClick you can have the following:
Intent intent = new Intent(this, ActivityB.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
In your application manifest file define the Activity like
<activity
android:name="com.example.taskandbackstackexample.BActivity"
android:label="#string/title_activity_b"
android:launchMode="singleTask" >
</activity>
and start your activity as
Intent i = new Intent(this, BActivity.class);
startActivity(i);
The activity's "launchMode" attribute has value "singleTask". The system creates a new task and instantiates the activity at the root of the new task. However, if an instance of the activity already exists in a separate task, the system routes the intent to the existing instance through a call to its onNewIntent() method, rather than creating a new instance. Only one instance of the activity can exist at a time.
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.