Mikkel Larsen
Mikkel Larsen

Reputation: 926

I want to return to a previous activity, even though I have code in it that skips it

Title might not be desciptive enough, but I had a hard time coming up with the title. Anyhow - here is the issue.

I've got this app. It starts off with an activity that is, a userguide you could say. Once you've read the guide, you go to the next activity. This tells you to insert your phone number. This phone number is then saved as a SharedPreferences on the phone. Then you move on to the next activity which is the main page. On the main page I've got a menu where you can return to the userguide if you forgot how the app works.

My problem lies here - both my userguide activity and the inserphonenumber activity makes a status check. Meaning, it checks if a variable is in the SharedPreferences, and if it is, it skips the activity.

if (number != NA) {
        Intent intent = new Intent(this, PhoneNumberActivity.class);
        startActivity(intent);
        finish();
    }

I've got this code in both activities. A method is obviously run before this to try and set the value number.

But now this means, that as I try to go back to the userguide from the frontpage, I've already got the number value and therefore it just throws me back to the mainpage.

How can I surpass this? Is there any smarter way to do this? The reason I'm checking for the variable in the first place is, that the userguide and phonenumberactivity should only show the first time the app is opened. The userguide is supposed to be accesible again, if people forgot how the app works.

I hope I made it clear, if not, please ask.

Hope you can help!

EDIT:

Manifest:

 <application
    android:allowBackup="true"
    android:icon="@drawable/ikon"
    android:label="@string/app_name"
    android:logo="@drawable/ikon"
    android:theme="@android:style/Theme.Holo" >
    <activity
        android:name=".UserGuideActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".FrontPageActivity"
        android:label="@string/app_name" >
    </activity>
    <activity
        android:name=".ChangeNumberActivity"
        android:label="@string/app_name" >
    </activity>
    <activity
        android:name=".PhoneNumberActivity"
        android:label="@string/app_name" >
    </activity>
</application>

Upvotes: 0

Views: 62

Answers (3)

Melquiades
Melquiades

Reputation: 8598

I suppose there are a couple of ways to achieve what you want. Another could be:

  1. When calling UserGuideActivity from FrontPageActivity, pass extra in an intent:

    Intent intent = new Intent(this, UserGuideActivity.class);
    intent.putExtra("user_navigated", true);
    
    startActivity(intent);
    
  2. In UserGuideActivity, first check for that extra, if it's there, you know you should show the guide. If it's not there, but number==NA, you also want to show the guide, because the app starts for the first time. Only when there is no extra in an intent, and number is already in preferences, you skip this guide. So in onCreate():

    //get calling intent
    Intent parentIntent = getIntent();
    
    if (parentIntent != null) {
        //let's check if the request is coming from FrontPageActivity
        //it is if it has extra "user_navigated"
        boolean showGuideUserRequest = parentIntent.getBooleanExtra("user_navigated", false);
    
        //if number is NA (meaning your app starts for the first time)
        //or showGuideUserRequest is true (meaning there FrontPageActivity has called this with extra)
        //then show the guide
        if (number == NA || showGuideUserRequest) {
            // show guide
        }
        //number is already saved in preferences, and this activity is not called from FrontPageActivity
        else {
            Intent intent = new Intent(this, PhoneNumberActivity.class);
            startActivity(intent);
            finish(); 
        }
    
    }
    else {
        Log.d(TAG, "parent NULL!!!!");
    }
    

NOTE: When you're showing the guide, make sure to know if you're showing it for the first time (then you want to navigate to PhoneNumberActivity after user has read the guide), or as request from FrontPageActivity (in that case you probably want to go back to FrontPageActivity after they've read the guide - think how you want to do that: by pressing back button, or by using the same method as if the app started the first time).

Upvotes: 0

denizt
denizt

Reputation: 713

Whenever you returned from your mainActivity class, it is obvious that you have the phoneNumber stored in SharedPreferences. And just before you start your userGuide Activity, set a global variable to something that you can interpret. After that, inside your userGuide activity; you should add a second check for your

if (number != NA) {
    if(Global.Set.equals("something you set"))
        //go on with userguide
    else {
          Intent intent = new Intent(this, PhoneNumberActivity.class);
          startActivity(intent);
          finish();
    }
}

and you will also use same method, to not go into your insertPhoneActivity and rather start your MainActivity.

Upvotes: 1

Chandrakanth
Chandrakanth

Reputation: 3831

You have to start the UserGuide activity from MainActivity by checking the shared preference variable. So at first time the app launching the UserGuide activity will show. From next time if user wants to see the user guide then provide an option like button then on clicking the button start the UserGuide activity.

Upvotes: 0

Related Questions