Question

I have an Activity name "IntroActivity" that is the "Launcher" activity for the app and after some decision making opens up a new Activity named "HomeActivity".

In my HomeActivity I have an ActionBar with a "Log Out" Action (which is in the menu but set to appear as an Action) which is supposed to do some operations and return the user to the "IntroActivity".

However, the startActivity method used inside HomeActivity does not work and there are no errors generated. I have debugged the code and the onOptionsItemSelected method is definitely executed as is the correct case inside. Here is the code:

HomeActivity:

@Override
public boolean onCreateOptionsMenu(Menu menu) {        
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_standard, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.menu_standard_settings:{
            // TODO
            return true;
        }

        case R.id.menu_standard_logout:{
            //Set logged in to false and then return the user to the intro page
            SharedPreferences.Editor editor = settings.edit();
            editor.putBoolean("logged_in", false);
            Intent introIntent = new Intent(this, IntroActivity.class);             
            startActivity(introIntent);

            return true;
        }

    }
    return super.onOptionsItemSelected(item);
}

The Manifest includes:

 <activity
       android:name="com.kennel39.diabeteslive_adtdev.IntroActivity"
       android:label="@string/app_name" >
       <intent-filter>
           <action android:name="android.intent.action.MAIN" />
           <category android:name="android.intent.category.LAUNCHER" />
       </intent-filter>            
 </activity>

I'm not sure what else I can provide that will be of use but I will quickly post up any extra parts if required. Thanks in advance, Josh

Was it helpful?

Solution

I think you miss commit the shared preference after setting loggedIn as false. So your flag is not updated and your intro activity again redirect you to home activity.

Try

editor.commit();

OTHER TIPS

From what I can see, you miss two things to make things work:

  1. Add editor.commit(); just after editor.putBoolean("logged_in", false); in order to save your value "logged_in"
  2. Add finish(); between startActivity(introIntent); and return true;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top