简体   繁体   中英

How do Activites follow eachother on Android studio?

So I am trying to learn some android Dev too and I am saving my data to sql and I am trying to save data on the signup but when data is successful it loads a different activity instead of main Activity this is my code. I want my method onSignupSuccess to open MainActivity.class I am not getting any Errors It instead opens

DisplayLogoActivity"

Manifest

<activity android:name=".activities.DisplayLogoActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".activities.MainActivity"
            android:screenOrientation="portrait" />
        <activity
            android:name=".activities.LoginActiviy"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme.Dark" />
        <activity
            android:name=".activities.SignupActivity"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme.Dark" />

My onSignupSuccess Method

  public void onSignupSuccess(int userId) {
        signUpBT.setEnabled(true);

        Session session = new Session(getBaseContext());
        session.setuserId(userId);
        setResult(RESULT_OK, null);
        finish();
    }

My MainActivity

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

        Session session = new Session(getBaseContext());
        if (session.getuserId() == 0) {
            startActivity(new Intent(this, LoginActivity.class));
        }

My Session

public class Session {

    private SharedPreferences prefs;

    public Session(Context cntx) {
        prefs = PreferenceManager.getDefaultSharedPreferences(cntx);
    }

    public void setuserId(int userId) {
        prefs.edit().putInt("userId", userId).commit();
    }

    public int getuserId() {

        return prefs.getInt("userId", 0);
    }

    public void logout() {
        prefs.edit().remove("userId").commit();
    }
}

You are starting the LoginActivity on your DisplayLogoActivity, when you finish the LoginActivity, the android will pop the "activity stack", so, the activity that will be shown is the DisplayLogoActivity.

To solve this, you can start your LoginActivity waiting for a result, with the startActivityForResult and, when you receive the result, if it's a OK, you start the MainActivity, or do whatever you want to do if the user is not logged in.

Another approach is to start your MainActivity on the DisplayLogoActivity instead of the Login. And use this code you have, to check if the user is logged in when the MainActivity is created, to start(or not) the LoginActivity.

To have a complete picture,

if you have activities A (DisplayLogoActivity), B (LoginActivity), C (MainActivity)

And if you call finish() method on B activity, just that activity will disappear from history stack, and last activity that has been opened before activity with finish() call, will be opened.

So in your case,

you have three possibilities to solve this problem (two from @Massita), and another one is to call finish() method also when you start B activity from A, something like this flow:

  1. Open DisplayLogoActivity
  2. Start activity LoginActivity from DisplayLogoActivity and call FINISH()
  3. On LoginActivity check is OK data for that user if yes, then start activity MainActivity and call FINISH()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM