1

This is part of my MainMenu.java

public class MainMenu extends Activity{

Button userinfo,requestservice,makepayment,trackparcel,checkcard;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_menu);
    // Show the Up button in the action bar.
    setupActionBar();
    userinfo = (Button) findViewById(R.id.userinfo);
    requestservice = (Button) findViewById(R.id.requestservice);
    makepayment = (Button) findViewById(R.id.makepayment);
    trackparcel = (Button) findViewById(R.id.trackparcel);
    checkcard = (Button) findViewById(R.id.checkcard);

    userinfo.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(MainMenu.this, UserInfo.class);              
            startActivity(intent);
        }
    });
    requestservice.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(MainMenu.this, RequestService.class);
            startActivity(intent);

        }
    });
    makepayment.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(MainMenu.this, Payment.class);
            startActivity(intent);

        }
    });
    trackparcel.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(MainMenu.this, TrackParcel.class);
            startActivity(intent);
        }
    });
    checkcard.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(MainMenu.this, CheckCard.class);
            startActivity(intent);
        }
    });

}

public void onBackPressed() {
    new AlertDialog.Builder(MainMenu.this).setTitle("Logout")
    .setMessage("Would you like to logout?")
    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) { 
            Intent intent = new Intent(MainMenu.this, Login.class);
            SharedPreferences sp = PreferenceManager
                    .getDefaultSharedPreferences(MainMenu.this);
            Editor edit = sp.edit();
            edit.clear();
            edit.commit();
            startActivity(intent);
        }
     })
    .setNegativeButton("No", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) { 
            // user doesn't want to logout
        }
     })
    .show();
}

This is part of my Login.java

public class Login extends ActionBarActivity implements OnClickListener {

private Button login, register;
private EditText email, password;

JSONArray loginposition = null;
// Progress Dialog
private ProgressDialog pDialog;

// JSON parser class
JSONParser jsonParser = new JSONParser();

private static final String LOGIN_URL = "http://XXX.XXX.X.XX:1234/PMSS/login.php";
// JSON element ids from repsonse of php script:
private static final String TAG_SUCCESS = "success";
private static final String TAG_MESSAGE = "message";
private static final String TAG_POSTS = "posts";
private static final String TAG_EMAIL = "email";
private static final String TAG_POSITION = "position";

@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    login = (Button) findViewById(R.id.login);
    register = (Button) findViewById(R.id.registerlauncher);
    email = (EditText) findViewById(R.id.userid);
    password = (EditText) findViewById(R.id.password);

    login.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            String Username = email.getText().toString();
            String Password = password.getText().toString();
            new AttemptLogin(Username, Password).execute();
        }
    });

    register.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(Login.this, Register.class);
            startActivity(intent);

        }
    });

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        // For the main activity, make sure the app icon in the action bar
        // does not behave as a button
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
}

public void onBackPressed() {
    Intent intent = new Intent(this, SplashScreen.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.putExtra("EXIT", true);
    startActivity(intent);
            finish();
}

I just don't know why After I Select Yes to Logout in MainMenu.java and it goes back to login page which is I want, But Once I reach Login Page, I press back again It will go back to main menu without having any logcat..

Jiazzy user
  • 119
  • 1
  • 15

3 Answers3

1

I think you need to call finish in the onBackPressed method of MainMenu. This will remove the activity from the back stack.

public void onBackPressed() {
    new AlertDialog.Builder(MainMenu.this).setTitle("Logout")
    .setMessage("Would you like to logout?")
    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) { 
            Intent intent = new Intent(MainMenu.this, Login.class);
            SharedPreferences sp = PreferenceManager
                    .getDefaultSharedPreferences(MainMenu.this);
            Editor edit = sp.edit();
            edit.clear();
            edit.commit();
            startActivity(intent);

            finish();  // Call finish here.
        }
     })
    .setNegativeButton("No", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) { 
            // user doesn't want to logout
        }
     })
    .show();
}
Rafa Viotti
  • 9,998
  • 4
  • 42
  • 62
0

Within your button press, swap your:

Intent intent = new Intent(MainMenu.this, Login.class);

for this:

Intent intent = new Intent(this, MainMenu.class);
apmartin1991
  • 3,064
  • 1
  • 23
  • 44
  • I have errors occur after change to `Intent intent = new Intent(this, MainMenu.class); ` which is **The constructor Intent(new DialogInterface.OnClickListener(){}, Class) is undefined** – Jiazzy user Dec 14 '13 at 18:59
  • 1
    That error is because `this` refers to the `listener`. You would want to use `v.getContext()` instead of `this`. However, this would start your `MainMenu` `Activity` which I'm pretty sure is not what you want. – codeMagic Dec 14 '13 at 19:05
0

As I found another way to end my application as I choose Yes to Logout from my MainMenu.java What another althernatives I can do is add public void onBackPressed() after onCreate() in Login.java

onCreate(){
 ...
}

public void onBackPressed() {
    finish();
}

This is another way I found that can works as well. But I think this way is not really recommended.

Jiazzy user
  • 119
  • 1
  • 15
  • 1
    Putting it in `onBackPressed()` is not necessary as pressing the "Back" button by default finishes the `Activity`. If you want it to be removed from the stack when starting the other `Activity` then that is where you need to use `finish()`. – codeMagic Dec 14 '13 at 19:14