I have activities A -> B -> C where A and B are for login and verification purpose. When a user reaches activity C, A and B are not necessary any more. That means if a user press BACK in C, the application should not go thru A and B, instead it should go back to HOME screen.
What's the conventional way to implement this in android? Thanks.
EDIT: to clarify a bit, user should be able to go back to A from B during login/verification phase, but not from C to A or B once the user reaches C.
When going to next activity call
finish();
before starting next activity.
Or, if not pressing back but going to next activity:
Intent intent = new Intent(this, A.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Using the following code:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
Intent data = new Intent(this, HomeScreenActivity.class);
data.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(data);
finish();
return true;
} else {
return super.onKeyDown(keyCode, event);
}
}
This way you override the back key behaviour and start a selected activity removing all other activities in between from the stack.
The simplest is to add
android:nohistory="true"
in your manifest for Activity A and B.
Whether or not the activity should be removed from the activity stack and finished (its finish() method called) when the user navigates away from it and it's no longer visible on screen — "true" if it should be finished, and "false" if not. The default value is "false".
A value of "true" means that the activity will not leave a historical trace. It will not remain in the activity stack for the task, so the user will not be able to return to it.
Related
The question is simple: Is there an intent FLAG that works exactly like pressing the back button? (making the activity slide from the left)
I need an intent to work exactly as moveTaskToBack(true) (if previous activity is in task stack).
I have tried calling Activity A (Main) with FLAG_ACTIVITY_CLEAR_TOP, but it doesn't get it "from the stack". I think it's creating a new instance, cause if I press back, I get the (on purpose unfinished) Activity B.
Note: I DON'T want to finish Activity B cause I need to go forward to it in some cases, without loosing the already loaded data.
You might want to call Activity B with FLAG_ACTIVITY_CLEAR_TOP if you want that to be on top of the stack and have A off of the stack.
If you never want Activity A to remain on the stack you can put in your manifest:
<activity
android:name=".ActivityA"
android:noHistory="true">
To go up to Activity A from your menu:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Intent intent = new Intent(this, ActivityA.class);
return(true);
}
return(super.onOptionsItemSelected(item));
}
If you are using you have to put:
android:launchMode="singleTask"
in manifest for your Activity B.
Hope this helps.
I have a question regarding android activity.
I have my first loginscreen(MainActivity) and from there go to Secondscreen, now when I am pressing escape button from the secondscreen I want to close the application(or android home screen) and next time open the application I want to start application with secondscreen.
i tried this code on my second screen but it does not working
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_ESCAPE)
{
//My logic here
return true;
}
return false;
}
First time when you use the application, Start with LoginScreen,
then call finish(). Also store a shared preference to know that, the
application is used atleast once(Logged in)
Since you finished the LoginScreen, when you press back button on SecondScreen, you will come to device home, not the LoginScreen.
Next time when you want to Start the application, check the sharedPreference whether the user is logged in, and finish the LoginScreen just at the start of onCreate
LoginScreenActivity:
onCreate(){
//check shared preference if logged in
if(yes){
startActivity(new Intent(Login.this, Second.class));
finish();
}else{
//do all the login here then,
//set sharedPreference
startActivity(new Intent(Login.this, Second.class));
finish();
}
}
What I assume you are trying something like below:
Frist installation#
LoginScreen>SecondScreen, when you press escape it will close entire app.
Second Time#
When you open app it will start from second screen instead loginScreen.
Solution
Phase1#
Store your login info in database or sharepreference when first time login has been made, and finish() loginscreen while opening second screen.
Phase2#
When you open second time your app, check your login info, if it is exists then redirect to second activity else open loginScreen.
When you start your second activity at that time finish the existing acitivity with finish()
example
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
finish();
This code will finish your current acitivity and when your press back button in second activity then the application gose close.
In log in activity call finish after starting second activity.
startActivity(i);
finish();
Now if u press back it should close the app and when u going to start app again the second activity will be visible.
There are two ways to do that:
make your log in activity with "noHistory=true" in android menifest activity definition.
<activity android:name="YOUR_FIRST_ACTIVITY"
android:label="FIRST_ACTIVITY"
android:noHistory="true" />
You can finish() the first activity at the time of starting second activity.
startActivity(SECOND_ACTIVITY_INTENT);
FIRST_ACTIVITY.this.finish();
Hope it will help you.
I have two activities, A and B. I have a button in A,when pressed starts a new intent to B.
However whenever I press the back button while in B, than click the button again is restarts Activity.I do not want to do that,I want it to resume Activity B.
Actually I am doing some networking in Activity B and I want to save unless the user wants to refresh.
How can I do it? Thanks in advance
Use
#Override
public void onBackPressed(){
moveTaskToBack(true);
}
Hope, It must help you
You need to Overrider the
onBackPressed
method and start there the activity like this:
#Override
public void onBackPressed() {
Intent intent = new Intent(this, activityA.class);
startActivityForResult(intent, PICK_CONTACT_REQ);
}
This sounds like you need to rethink your architecture. You say:
Actually I am doing some networking in Activity B and I want to save
unless the user wants to refresh.
If this is the case, you probably don't want to do your networking in ActivityB. Maybe you should start a Service from ActivityB to do the networking. The service and the activity can communicate with each other so that the activity can keep the user up-to-date about the state of the networking. If the user presses BACK in ActivityB, it can finish (as usual) and return to ActivityA. In this case, the Service is still managing the networking. When the user again starts ActivityB (from ActivityA), the new instance of ActivityB can communicate with the service to see if there is any networking going on, and if so it can get the current status of that networking or start it or stop it or whatever.
I guess I'm too late but I had a similar problem.
I had two activities A,B and a next button in A.
Whenever I tried to do: A->press next button ->B->press back button->A->press next button->B, B screen got destroyed when I pressed the back button. So when I came back to B for the second time it was a newly created B (all the information I had put in was gone).
So it was like A->B->A->new B when I just wanted to go back to the original B! :(
So what I did was, in activity B, I overrode the onBackPressed() function so it doesn't destroy the activity. I also set the flag so that if there is a A activity already running, it would just pull it up to the front instead of creating a new A activity.
#Override
public void onBackPressed() {
Intent intent = new Intent(getApplicationContext(), ActivityA.class);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
}
Then, for the onclicklistener function for the next button in activity A, I set the flags similarly.
public void onClickNextbutton(View v){
Intent intent = new Intent(getApplicationContext(), ActivityB.class);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
}
Is there any way to kill the app as soon as it goes to background. For example, if I press the home key and again start the app, it goes to same activity but what I want is that as soon as app goes to background, it should be killed or it should start from the first activity.
it is not "killing" your app perse, but this will give you the effect you are after.
make the intents used to start your second-Nth activity with the FLAG_NO_HISTORY. That way they will be finished as soon as they leave the screen.
Intent i = new Intent(this, SomeOtherActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(i);
When your activity leaves the screen it will be finished, which will cause it to launch from the starting activity the next time it gets launched from the home screen.
You can catch the event when the user presses the home button as follows
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_HOME)) {
finish();
}
return super.onKeyDown(keyCode, event);
}
You can also override the onBackPressed() method in your activity and call finish from there.
I have this Activities sequence:
Avtivity01 => Avtivity02 => Avtivity03
You can go from Avtivity01 to Avtivity02 after you click a button.
You can go from Avtivity02 to Avtivity03 after you click a button.
-
I want to go from Activity03 to Activity01 DIRECTLY after I click a button.
-
NOTE:
I do NOT want to use Intent, because I want to have Activity01 as if I pressed the back button from Activity02
How to do that, please?
Why can't you use an Intent? You can use the FLAG_ACTIVITY_CLEAR_TOP when you click the button. Edit: If you want to preserve the original instance of the Activity, you can use this flag in conjunction with FLAG_ACTIVITY_SINGLE_TOP.
Do you ever want to be able to press a button from Activity03 to go back to Activity02? If you ALWAYS want it to go back to Activity01, you could alternatively use android:noHistory="true" on Activity02 in the manifest and just call finish() on Activity03.
You can go back more than one screen to which ever screen you need using intent and use flags to prevent going back to same screen :
Intent gotoScreenVar = new Intent(goFromScreenCls.this, goToScreenCls.class);
gotoScreenVar.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(gotoScreenVar);
I was had the same problem,
Short answer is add this line to intent.
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
Long answer is:
I have MainActivity, SecondActivity and ThirdActivity what i want to do is opening SecondActivity from MainActivity and then ThirdActivity from SecondActivity and i want to click a button in the ThirdActivity that close both of SecondActivity and ThirdActivity without calling a new MainActivity i want it to functional as i back to it not creating a new one And the most thing i need is to keep back button functional normal in the ThirdActivity to back to SecondActivity if i need to change some of my choices in the SecondActivity so i can't call finish(); in the SecondActivity after starting ThirdActivity or any other solutions like android:noHistory="true" in the SecondActivity manifest because those solutions kill the SecondActivity and i can't go back to it throw back button.
so this flag solve the problem for me FLAG_ACTIVITY_REORDER_TO_FRONT
FLAG_ACTIVITY_REORDER_TO_FRONT
If set in an Intent passed to Context.startActivity(), this flag will cause the >launched activity to be brought to the front of its task's history stack if it >is already running.
go to reference here
so i tried this code to go back my MainActivity from ThirdActivity when i click the button in ThirdActivity . i hope this will help you.
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
link has good article. This will help you. By the way you can use FLAG_ACTIVITY_REORDER_TO_FRONT to solve your problem.
You can override Back button click on Activity3
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
startActivity(Activity3.this,Activity1.class);
moveTaskToBack(true);
return true;
}
return super.onKeyDown(keyCode, event);
}
It will work with just one line.
after calling your third activity call finish() to next line. and it will work.
eg.
In your Activity02
Intent thirdActivity = new Intent(this,Activity03.class);
startActivity(thirdActivity);
finish();