how to clear background data when sign out - android

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);
}

Related

Android back button after clear task and set new task

So I have three activities A, B, and C (A>B>C). Before I startactivity(C) in B, I clear task and set new task to make C become a new root. Everything works as expected. When user clicks the back button in C, they go back to the main screen, but when the user clicks the app again, it redirects them to A. My thought is that there is nothing in the stack, so it just shows the default activity? How can fix this? so that when user clicks on the app again, then it will show C activity?
Which Activity is going to launch through clicking is defined in AndroidManifest.xml, see this official tutorial
If all you need is to stop user from going back to Activiy A and B, maybe you can just override the onBackPressed() and don't clear tasks. In this way you can still have user navigated to C when the icon is clicked.

Android: Closing the application and opening it from recent list

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...

Is there a way to detect which Activity will be resumed upon hitting the back button?

My app begins with Activity A (log in screen) which I always want on the bottom of the Activity stack.
Based on some logic after login, I launch some other Activity B-Z. There is a menu in the app which allows you to switch around between any Activity B-Z.
If the user hits the back button enough times, I don't want them returned to the login screen. I want the user to be sent to the Android Home screen if the back button is pressed on the Activity which has Activity A as the next Activity on the stack. This Activity is not guaranteed to be the Activity which was launched by Activity A because my Activities use singleTop.
Ideas?
The only other option I can think of is to remove singleTop and whatever Activity is launched by Activity A could remember that (my Activities all derive from a base class, which I would use to do that).
Another possibility may be to do something like the following in the onBackPressed handler:
if (getParent().getClass().getName().equals(ActivityA.class.getName())) {}
Although not a direct answer to your question, but if the problem is that
I don't want them returned to the login screen
then the classic solution is to finish() the login Activity when the user has logged in successfully. This way you'll make sure the user will never return to that Activity.
To do this, Why don't you get the User Login information, and store it in your apps private shared preferences. Then when the User launches your application, you can read the Shared preferences from activity A, and automagically log them in to activity b .
something like this:
SharedPreferences myPrefs = getBaseContext().getSharedPreferences("MyPrefs", SharedPreferences.MODE_PRIVATE);
if((myPrefs.getString("username",null) != null) && (myPrefs.getString("password",null) !=null)){
// Make sure your user is a member of your application
// auto log in to the home page
Intent bIntent = new Intent(getBaseContext(), BIntent.class);
startActivity(bIntent);
}
For more reference on how to use Shared Preferences
http://developer.android.com/reference/android/content/SharedPreferences.html
Then if you want to be really slick, Store the Class name when the onDestroy() method is called in each Activity, overwriting each class as the last open Activity. So whichever activity the user was last on before the application was closed is stored in preferences and you can read that in the login activity, then from there launch b-z
This will make it so that the login activity is always in memory, since it is first launched to check your users credentials.

Issue on Handling Back Button

My App starts with a splash screen loading background data from remote server. After completion of data load, An Activity(say B) launched.
This activity B (A photo Gallery of different Animals), when pressed back launches SplashScreen, so inorder to solve this, i prompt user if it really wants to exit the app, if clicked yes, exit closes.
private void exitApp(){
B.this.finish();
}
My issues comes here.
Since Activity B, when clicks on Particular Animals say DOG,
Intent intent = new Intent(B.this,C.class)
startActivity(Intent);
takes the user to Activity (C) reviews of particular Animals.
When back button is pressed on C, takes me to B, thats fine.
Since B has menu options, such as bookmark,Home.
i do Launch BOOKMARK as with
sartActivity(B.this, Bookmark.class);
Since BookMark has menu for HOME i.e Activity B.
And now when i pressed back, it prompts me to Exit but it does exit the app rather takes me to BOOKMARK.
Can i solve this issues? As i was reading the doc, i found
`finishActivityFromChild(Activity, requestCode);`
can this help me achieve?
Let say when a user is prompted to exit the application. I simply want to remove all the stack of activities.
Can it be done?
add below tag in your AndroidManifest.xml file under BookMark activity
android:excludeFromRecents="true"
and include below tag in your HOME i.e Activity B.
android:launchMode="singleTask"
and let me know if it worked for you.

Android - Clear existing objects and variables in memory when returning to Log In screen

I have an issue where if my user is on the Dashboard screen and presses the phones 'Back' button, this will then return them to the login screen (which still has their details input) and if they login again, some variables are global so these are then effectively reused which effects the functionality of the application.
My thinking was I could override the onResume method when this activity is resumed and then clear everything but I am unsure on how to code this and clear the form and any variables still existing in the applications memory.
Thanks.
finish() your login screen when you logged in.
For example:
//I'm logged in, starting dashboard view
startActivity(intent);
//finishing login activity - I don't need it on back stack
finish();
The safest option would be to call finish() in the login activity after switching to the new activity. This will prevent the activity to go back to the login screen after pressing the back button, as that will remove that activity until it is manually started again.

Categories

Resources