Reputation: 1934
I have seen a bunch of posts like this, but none of the solutions has worked for me. I have an app, where the first thing that is loaded is the LoginActivity, it checks to see if you are already logged in, if you are it send you to the MainActivity, which is the main meat of the application. My problem is, when you are on the MainActivity and you hit the hardware back button, it just reloads the MainActivity because, I'm assuming it tries to go "back" to the LoginActivity.
I have tried various "fixes" from posts I have seen on here, but none of them seem to work. Currently in the AndroidManifest.xml file for my MainActivity I have added the
android:noHistory="true"
To the MainActivity intent, but that isn't making much of a difference. I have seen another post where someone mentioned basically starting a new Activity with a special Intent that makes the system load your home screen, but someone pointed out that if someone loads your app, hits back, then does it again it keeps adding to the "stack" and that can be very bad.
So I am hoping someone has encountered something similar, where you have to have the LoginActivity load first to verify the user, then that launches Main, but then you need to kill the app on back button press from the MainActivity.
Thank you for any help in advance. I appreciate it.
Upvotes: 1
Views: 1392
Reputation: 1627
Create a function onActivityResult
to your LoginActivity
and call the MainActivity
using the startActivityForResult()
EXAMPLE:
LoginActivity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startActivityForResult(new Intent(this, MainActivity.class), 1);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (data.getBooleanExtra("EXIT", false)) {
finish();
}
}
}
To close the entire app fromMainActivity
use theonBackPressed
. don't forget to remove the super.onBackPressed()
MainActivity:
@Override
public void onBackPressed() {
// super.onBackPressed();
Intent intent = new Intent();
intent.putExtra("EXIT", true);
setResult(RESULT_OK, intent);
finish();
}
Upvotes: 3
Reputation: 1791
You can try my code :
void gotoLogin() {
Intent t = new Intent(SplashScreenActivity.this, LoginActivity.class);
finish();
startActivity(t);
}
and
void gotoMainActivity() {
Intent t = new Intent(SplashScreenActivity.this, MainActivity.class);
finish();
startActivity(t);
}
after you check
if ( you logged in){
gotoMainActivity();
} else {
gotoLogin();
}
I hope it can help your problem! Thank you!
Upvotes: 0
Reputation: 755
I have an app that the first screen is a splash activity(shows my logo) and after some seconds it goes to MainActivity. In your LoginActivity close your LoginActivity after starting your MainActivity. do something like this:
Intent mainIntent = new Intent(LoginActivity.this,MainActivity.class);
LoginActivity.this.startActivity(mainIntent);
LoginActivity.this.finish();
Upvotes: 1
Reputation: 526
When I have this kind of issue, I'm adding in "AndroidManifest.xml" for every Activity this :
android:launchMode="singleTask"
In your loginActivity :
startActivity(new Intent(this, MainActivity.class));
finish();
Check this : https://developer.android.com/guide/topics/manifest/activity-element.html
In contrast, "singleTask" and "singleInstance" activities can only begin a task. They are always at the root of the activity stack. Moreover, the device can hold only one instance of the activity at a time — only one such task.
Upvotes: 0