I know this question has been asked so many times on SO, so take a moment before you down vote it. I have gone through How to remove application from recent application list?
Quitting an application - is that frowned upon?
Close application and remove from recent apps
And many other questions on SO.
In my application I'm storing the user login details in SharedPreferences. Basically when my application starts - Activity A - Splash screen will show up from 3 seconds. Then it will go to Activity B - The user login page, on press of login the credentials are stored in SharedPreference. Then it will go to Activity C - Home page.
I don't want the user to enter the login details each time so I'm storing them in SharedPreference, if the value is present then redirect directly to Activity C - Home Page.
So the flow is like this,
If SharedPreference is empty
A->B->C
If values are present in SharedPreference
A->B->C, I'm checking in Activity B and when the values are present redirect it to C without making the B to come to front.
This all works fine if I open application from launcher. But if I open it from recent list even though the values are present in SharedPreference it is going to Activity B. And when I open it from launcher or recent list the Activity A (splash screen) doesn't show up. Only if I logout and open the application it shows up. What would be the problem?
I tired putting <activity android:excludeFromRecents="true"> so forcing it to not to appear in recent list. Even after this if I open it from launcher it goes to login page i.e Activity B.
I'm overriding the back button like this on Activity C
public void onBackPressed() {
if (backPressedOnce) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
super.onBackPressed();
return;
}
this.backPressedOnce = true;
Toast.makeText(this, "Please press back again to exit.", Toast.LENGTH_SHORT).show();
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
backPressedOnce = false;
}
}, 3000);
}
public void onResume()
{
super.onResume();
this.backPressedOnce = false ;
}
So I have two questions
Q1) How to make splash screen appear each time the user launches it?
Q2) If launched from recent list, why it is not redirecting to Activity C even when SharedPreference has values?
Sorry to ask very lengthy question, I know it is not appropriate.
Thank you
i will put my thoughts...
why r u checking sharedpreference in B at all? if u have stored in sharedprefernce redirect the user directly to C from A .
if(sharedPreference) -> C
else B -> C
when u are launching from recent afaik it will call onresume and not oncreate and also i believe you are checking in oncreate hence its not executing when launching from recent , use logs to know the cycle also please refer to activity life cycle.
if u dont need the activity to exist u can call finish() and also put noHistory='true' in manifest...
plz see to these points and rectify me if i m wrong anywhere...
thx and hope it helps u...
Related
I have been writing a multiple accounts manage module. What I want to do is to lead user to Accounts list activity when signing out. The problems is when user press back button, it resumed the background activity and user still can use the app even signed out. And it doesn`t work either when I cleared account info in database and SharedPreference.
For better understanding I describe the problem again.
For example, I have three activities, A, B and C. A works as main activity with a list, B works as the settings menu activity and C works as the account list Activity.
When I navigate from A to B click "Sign Out" menu in B, then the process flow goes from B to C.
Because it asks user to choose (if has) or login an account.
Now the problem is when user press back button, it can go back from C to A rather than exit the app (go to home screen). See the screenshot blow.
Since user already signed out, I doesn`t make sense to navigate back from C to A. C should be the only visible Activity at this circumstance.
But I don`t know how to implement this, I already clear account info in local storage, so it should not be the settings problem.
So how to clear the background activity A?
Any comments will be greatly appreciated.
Maintain a flag suppose "isLoggedIn" in shared Preferences or database. In your launcher/first activity check this flag and decide whether to call login activity or direct xyz activity. And when you clear data again call your launcher/first activity with clear_top flag in intent.
try this code in Activity C
#Override
public void onBackPressed() {
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startActivity(startMain);
}
Basicly, i have 3 activities in my app. call them A,B and Login, A and B shows the user very sensitive data. a legit flow has the form of launch->Login->A->B->A->B...->A->quit what i really want is that if for some reason the app got to the background ( by pressing back or home or whatever ) while it was still on A, or on B, then no metter what way the app relaunches ( or traced in some way including by the long home press menu ) it would not show A or B content. he can see either the login page content, or nothing at all.
noHistory is almost what i was looking for, but isnt a good solution since i want to allow intuative navigation from A to B and back to A. so how is it done?
Check out the "clearTaskOnLaunch" Activity attribute.
When the value is "true", every time users start the task again, they
are brought to its root activity regardless of what they were last
doing in the task and regardless of whether they used the Back or Home
button to leave it.
try the following pseudo:
public A extends Activity
{
boolean just_launched = true;
void onPause()
{
just_launched = false;
}
void onResume()
{
if (!just_launched)
finish();
}
}
I have a simple app that has 2 Activites cMain and cPuzzle. I would like it so if a user goes to another app, and then back to my app. My app will alwys be at the cMain activity.
Right now in my cPuzzle activty, I have the following code to go to the cMain actvity when the user is returning from another app.
protected void onPause()
{
super.onPause();
finish();
}
Right now I'm testing it on a kindle. What happens is that I run my app, go to the puzzle activity, then go to another app. When I retutn to my app, there is just a blank screen.
Because you finished your activity and do not start another. Use intent for start needed activity cMain
I am creating an application in which first of all user login in the second activity and moves to third activity.
If user directly quits from second page i have used movetasktoback(true) so application closes from third activity.
If user again open application then third activity is shown directly for this in main(first) activity i have stored user information in preferences which decide that either to go on second activity or login screen or to third activity.
if user want to close application i have a diff activity which logout the application and moves to second activity.if user closes application from login screen i have used movetasttoback true which easily closes the app.
Everything is fine till now. but if user again start application after 1 hr to login as user login the screen it opens fifth or sixth activity which was last opened and application behave in bugging way.
Please tell how to resolve this.
Thanks.
Ok so do one thing when you close your application actually it runs on background.
After opening the application you lancing a new task for that put on appropriate class's:
#Override
public void onBackPressed() {
Intent setIntent = new Intent(Intent.ACTION_MAIN);
setIntent.addCategory(Intent.CATEGORY_HOME);
setIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(setIntent);
ClaasName.this.finish();
I hope that will helps you.
in my activity I have login page (L), which leads to hierarchy of activites (L -> A -> B -> C). When user log in, and he goes up to activity C, he minimizes his app and after some while, system will do a force close on this app.
Now, when he start this app again a he log-in, he should have see, where he ware last time an application was running with all opened activities on stack (if he was in C, one back button leads to B, then to A, then to L). How to achive such a behavior in Android? I am now using sharedpreference, which is hodling string of visited activities, then some flag, which tells me whether an app was finished with System close or user close and them I am persisting each activity with its own sharedpreference. If system kills my app, after login I open series of past opened activities in For cycle, but they are on the stack only. They are opened (= onCreate method is run) only when I use back button to see them.
Do you see any cleaner approach?
Thanks
When I want to maintain hierarchy after a force close I bypass the activity stack by replacing each activity with the one that it calls and then override the back event to do the same in reverse.
In ActivityL.java, where you want to go to ActivityA:
Intent intent = new Intent(getApplicationContext(), ActivityA.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
You now just have one activity on the stack - ActivityA. To have the back button behave properly and return you to ActivityL, add this to ActivityA:
#Override
public void onBackPressed () {
Intent intent = new Intent(getApplicationContext(), ActivityL.class)
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
}
You'll still need to store enough state information to get you back to the activity where the user was before the force close. In onCreate for ActivityL, check that state info to determine which view you want the user to be on and use code similar to the first block above to go directly there.
This seems a bit cleaner to me than rebuilding the entire activity stack on startup. This does however become more complex if your activities don't follow a strict hierarchy. (i.e. sometimes activity A starts activity B and sometimes it starts activity C).