I have 3 activities:
- LoginScreen.
- MainScreen.
- ProfileScreen.
The loginscreen is the main activity which gets called first time and at the clicking of login button I call the mainscreen开发者_高级运维 activity by using following code:
finish(); // I am calling finish because I don't want user to press back key at mainscreen and get back to login screen.
Intent it = new Intent(LoginScreen.this,MainScreen.class);
startActivity(it);
In my mainscreen I am having a logout button and I am using this code for loggin out:
finish();
Intent it = new Intent(MainScreen.this,LoginScreen.class);
containerObject.startActivity(it);
I don't know why but this code doesn't take me to login screen.
If I change the code from above to this code :
finish();
Intent it = new Intent(MainScreen.this,ProfileScreen.class);
containerObject.startActivity(it);
The code works absolutely fine and takes me to profilescreen.
Let me make it clear that I haved added all 3 activities to manifest.
Am I getting this problem just because "LoginScreen" is the main activity?
Better late than never.
In your logout button click handler you have to start the activity using containerObject, as you have already finished the MainScreen activity, so you have to go to the parent container of your activity.
logoutButtonClick(../..[
finish();
Intent it = new Intent(MainScreen.this,LoginScreen.class);
containerObject.startActivity(it);
}
To ensure the activity is finished call this.finish within the on click handler, but after starting your intent.
logoutButtonClick(../.. {
startActivity(new Intent(this, LoginScreen.class));
this.finish();
}
Then override your onBackPressed event when in the LoginScreen activity. So the backpress will keep taking the user to the login screen.
@Override
public void onBackPressed() {
startActivity(new Intent(this, LoginScreen.class));
this.finish();
}
I'm not sure if how you are wanting to manage your navigation for the duration of the app between the three activities so I won't go into more detail to navigate between the ProfileScreen activity and the other two, but I'm sure you have enough information to move forward with this.
When I'm wanting to secure a password protected area of my app, I override and manage all my backpress events and carefully manage the navigation throughout the app and the application lifecycle, so an app cannot be closed and reopened on an activity that is secured with a password.
精彩评论