-3

I have a Android application that looks like this:

LoginActivity -> MainActivity

My Android problem is that if we have successfully logged in (we are at MainActivity), then press the Back Button on the phone, we will go back to LoginActivity. That doesn't make any sense, because we are logged in.

How can I check in LoginActivity if we came from Back Button? I tried this code, but it doesnt help, because onCreate is not run when coming from Back Button.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.login);

    // Check if we are registered before, then just go to Main
    checkIfWeAreRegistered();
}
Solo
  • 569
  • 1
  • 7
  • 27

6 Answers6

2

it just simple just call finish() like this

Intent intent = new Intent(LoginActivity.this, HomeScreenActivity.class);
startActivity(intent);
finish();

or try this

 Intent intent = new Intent(LoginActivity.this, HomeScreenActivity.class);
 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
 startActivity(intent);
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
1

Before start main activity just finish LoginActivity once login success.

 Intent intent = new Intent(LoginActivity.class, MainActivity.class);
 startActivity(intent);
 finish();

other wise Activity 2 customized onBackPressed in MainActivity

 @Override
public void onBackPressed() {

// do your stuff
}
sasikumar
  • 12,540
  • 3
  • 28
  • 48
0

Instead of checking for the onBackPressed call in your LoginActivity you can set in the manifest noHistory=true to remove it from the backstack.

Murat Karagöz
  • 35,401
  • 16
  • 78
  • 107
0

You can uses startActivtyForResult() for activity transition and handle onActivityResult in your login activity. An you close the activity from this override method.

Note: You should call setResult(RESULT_OK) form back button press from your mainActivity.

EKN
  • 1,886
  • 1
  • 16
  • 29
0

Try to check in onBackPressed method in MainActivity if you have already registrited. If so do not go to LoginActivty.

 @Override
public void onBackPressed()
{
     // code here 
     super.onBackPressed();  
}
Dmitry Ushkevich
  • 392
  • 2
  • 14
0

@Solo,

Once you go from LoginActivity to MainActivity your LoginActivity goes to backstack and hence on clicking on the back button in MainActivity it calls the LoginActivity again from backstack, in which it calls onStart() method.

There are multiple way by which you can avaoid user going back to LoginActivity from MainActivity

First

finish LoginActivity after you call MainActivity

so for example

Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish(); // will finish that activity.

Second

Intent newIntent = new Intent(this,MainActivity.class);
newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(newIntent);

Here Intent.FLAG_ACTIVITY_NEW_TASK clears the stack and makes it the top one.So when we press back button the whole application will be terminated.

eLemEnt
  • 1,741
  • 14
  • 21