0

Ok first, let me say, I have read many threads here, and elsewhere, but can't seem to find the answer here. I realize that "exiting" and app is frowned upon, but here is my scenario, what should I do?

  • First launch, customer sees login screen, with option to remember me
  • after Login, menu activity is activated
  • I want the back button to exit the App from the menu, not go back to the login activity that the app starts on. I have a menu option to "logout" if they need to get back to the login screen, otherwise I want to just exit the app on back press from the menu activity.

Is there a way, in my override of OnBackButtonPressed, that I can trigger the app to exit?

Ram kiran Pachigolla
  • 20,897
  • 15
  • 57
  • 78
Wanabrutbeer
  • 676
  • 6
  • 11
  • If your login screen is a fragment and the app is launched in the same activity, you can simply not call `addToBackStack()` when removing the login fragment. – anthropomo Feb 01 '13 at 03:08

2 Answers2

0

Why not start the activity C with a flag in your Intent? I don't think you will need to override the OnBackButtonPressed. Too complex for a rather simple solution.

For example:

Intent showActivity = new Intent(Sender.this, Receiver.class);
showActivity.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(showActivity);

This way, when the user presses the back button, he will exit the application and not go back to the Login Screen.

If you still need to override the Back Button, then try this example:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        Intent a = new Intent(this,A.class);
    a.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(a);
    return true;
    }

    return super.onKeyDown(keyCode, event);
}

Props to bitestar for this piece of code here: https://stackoverflow.com/a/9398171/450534

Community
  • 1
  • 1
Siddharth Lele
  • 27,623
  • 15
  • 98
  • 151
0

I have read all of your requirements,you need to just manipulate the flow of application as I have shown below to achieve it.I hope it will be helpful to you.

Inside your login screen which is going to be launched while starting the application,

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.login);
    UTILS.Log_e(TAG, "onCreate");




        initialize();

    //check for configuration
//here you need to check,whether user has been logged in already or not
    if(!CONSTANTS.isConfigured)
    {
//display login dialog if user has not been logged in already

        displayLoginDialog();
    }
    else
    {

//move to Home screen if user has already logged in
        i.setClass(mContext, HomeActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(i);
        finish();
    }

}

Inside your Home screen,

    @Override
public void onBackPressed() {
    exitApp();
}

private void exitApp()
{
    i.setClass(mContext, ExitActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(i);
    finish();
}


//call logout function as per your requirement

    private void logout()
{
    i.setClass(mContext, LoginActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(i);
    finish();

}

Inside your ExitActivity,

   public class ExitActivity extends Activity{

    private static final String TAG = "ExitActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        moveTaskToBack(true);
        finish();
        }

}

NOTE: in above codes, I have used i.setClass(context,destination_class_name) in which i is the instance of Intent.

Inside your manifest,

        <activity
        android:name=".ui.LoginActivity"
        android:label="@string/app_name" 
        android:windowSoftInputMode="stateHidden|adjustPan">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".ui.HomeActivity" >
    </activity>
    <activity
        android:name=".ui.ExitActivity" >
    </activity>

Kindly let me know if you face any problems or if you don't understand any point in above code.

Mehul Joisar
  • 15,348
  • 6
  • 48
  • 57