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.
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);
}
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...
In many android application the first activity can be either a login activity or the home activity (if the user is already logged in).
So the question is, which activity start at startup? Can be a good idea start always the login activty and in the onCreate test if the user is already logged in? For example:
onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
if(isUserAlreadyLoggedIn())
{
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
}
}
The activity you declare in Manifest to be your main activity is not necessary the first activity that will start when your application will be created.
Lets assume you have two activities Menu and Login, and you set your Login activity to be your main in manifest.
In case the first time you lunch your application you will get to Login activity. But if the user hit the home button while in Menu activity, and than also kill your application process, ether by visiting other activities until there is not space for your Menu activity or by manual killing the process using Task Killer(Note that there is deference between closing the application from settings with Force Stop, as it also clears the activities stack and using Task killer as Advanced task killer on Android Market). In this case the next time the user lunch your application, the first activity will be the Main menu.
As for user experience it's best to show him the Login activity more than once, just remember his details after his first successful login.
Your code seems valid to me.
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 have a typical Android app where the user has to log in before displaying the main Activity of the application. However, if we've already got a cached username / password, I want to skip the login page.
I could, in my Login onCreate, detect if I've got a user/pass and push a new Activity, but I'm worried that this will make my app startup slower (since I have to load an activity then immediately throw it away), and it also perhaps breaks the back button (i.e. you can hit back and end up back at the login screen).
Is there any way to avoid this and only load the full Login Activity if there is no cached password?
You can make a separate helper activity which launches either your login activity or your main activity. In its onCreate, you'd use startActivity and immediately call finish to remove the helper activity from the back stack.
Intent intent;
if ( /* already logged in */ ) {
intent = new Intent(this, MainActivity.class);
} else {
intent = new Intent(this, LoginActivity.class);
}
startActivity(intent);
finish();
Then, LoginActivity should re-launch MainActivity as normal. This way, the user will never be able to navigate back to the helper activity as it does not appear in the back stack. Do note however that the user can still login, go to the MainActivity, pause that activity, remove his account (through the Android settings) and resume the activity (from the recent apps). If you want to prevent this, you're probably better off placing the login redirect in MainActivity instead (perhaps even in onResume).
You could create a Splash Activity.
On the AndroidManifest.xml's Splash Activity tag, add the android:noHistory="true".
On the Splash Activity, check whatever you need (maybe with an AsyncTask if it may take a long time, to avoid freezing the Activity) and, depending on the result, you start the Login Activity or any other Activity.
Another approach is to transform login screen from Activity to Dialog. That should be easy. And then make your main activity check if there is cached username/password or not. In the second case show LoginDialog to user.
I also have a typical Android app with the same requirement, and I solve it as follows.
First I configure the initial launch activity to be MainActivity, and then inside onCreate(), check if the user has saved credentials (this is all done through AccountManager and authenticator service).
If the check fails and user needs to authenticate, then I start the LoginActivity. If logging in fails or the user presses back, the MainActivity calls finish() on itself to close the whole app. Otherwise, after logging in, the MainActivity is resumed and is presented to the user properly.
The advantage of doing it in this order is that (in my case anyway) the user is more likely to be logged in than out, and so this will avoid any start/stopping of unneeded activities as you say.
A second advantage is that, if the MainActivity (or another activity) is resumed at a later time and the user's session has expired, you can simply start the LoginActivity again to reauthenticate them.
Hope that all makes sense :)