Loop to start activity based on condition
I'm trying to write an android application that has two main activities: login screen and display screen. Currently I'm setting it up so that if the user is already logged in, the login screen will be skipped.
In order to work out th开发者_Python百科e best way to do this, I created a generic "Main" activity that looks something like this:
public class Main extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
// Get preferences
SharedPreferences settings = getSharedPreferences("PREFERENCES", MODE_PRIVATE);
super.onCreate(savedInstanceState);
//setContentView(R.layout.viewusage);
//System.out.println(settings.getBoolean("LOGGEDIN", false));
if (settings.getBoolean("LOGGEDIN", false) == false)
{
Intent intent = new Intent(Main.this, Login.class);
startActivity(intent);
}
else
{
Intent intent = new Intent(Main.this, Display.class);
startActivity(intent);
}
}
}
The idea is when someone logs in successfully, the login activity will save all the details needed to the user preferences, and then finish() the activity. This will return to the Main activity. What I'm trying to do is have the main activity keep looping through the if ... else statement when there aren't any activities running: so when the login page closes, it will re-run the if ... else statement, and should launch the Display activity rather than the Login activity. The reverse will be true if the user logs out from the Display class.
I've thought of using a while (true) loop, but that just hangs the program. I've thought of putting it in a separate thread, but I don't know how to detect if there is already another activity running, so a separate thread will just keep looping and opening new activities (I presume, I haven't tried it)
Any suggestion on how I could work this out?
Thanks.
You can't do it by looping in onCreate
, because the activity doesn't actually get off the ground until onCreate
returns.
One possibility is to use startActivityForResult
and arrange for the Login and Display activities to return a code indicating whether to quit or proceed to the other activity. Then you would place your logic in onActivityResult
instead of looping in onCreate
.
Another, perhaps cleaner approach is to design the Display activity to directly start the Login activity when the user logs out, and vice versa. That way, the Main activity can just call finish()
after it determines which activity to display initially.
Off the top of my head.
Write a login class that timestamps the login and expires the login after a set time interval perhaps with a method isLoginStillValid() and save the login object on a soft kill in onRetainNonConfigurationState. Retrieve the login object in onCreate. Save the login fields, if applicable to preferences in onDestroy(). Set a flag isSavedInstanceState to true in onRetainNonConfigurationState and use this to determine if you need to write to prefs in onDestroy. I know this sound complicated, but this is a reusable template.
So in the end, in onCreate, look for the login object. Check to see if the login has expired. If so, post a GetLogin dialog. In each credentialed call, check to see if the login has expired before allowing access to the credentialed call. If the login has expired, consider posting the GetLogin dialog again. Remember, time may have passed since the app came to the foreground.
Finally, launch GetLoginDialog using startActivityForResult so that you can retrieve the login data and update the login object with a new timestamp.
JAL
I'm a little confused by your question, but I think you want to use startActivityForResult
rather than startActivity
. This will allow you to return a true
/false
result from your Login Activity
so that you can then launch your Display
Activity
if the login succeeded. Similarly for your Display
Activity, if the user logs out, you would finish()
the Activity
and return a "logout" result to Main
that logs the user out.
精彩评论