我需要我的应用程序来检查它是否第一次运行。如果是第一次发射,那么应该发射LoginActivity
而不是MainActivity
。如果不是第一次运行,它应该像往常一样显示MainActivity
。
我使用SharedPreference
值来检查它是否可用,然后应用程序决定它不运行它的第一次运行。
这是我到目前为止所尝试的
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set default values into settings
PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
// Check if the app is running on its first run
SharedPreferences fRun = getPreferences(MODE_PRIVATE);
if(fRun.getBoolean("firstrun", true)){
SharedPreferences.Editor editX=fRun.edit();
editX.putBoolean("firstrun", false);
editX.apply();
// Login activity stuff here
// Goto login screen
Intent loginIntent=new Intent(getApplicationContext(),LoginActivity.class);
startActivity(loginIntent);
//finish();
} else {
setContentView(R.layout.activity_main);
}
}
}
我的问题是,当我运行我的应用程序时,它突然崩溃并显示消息Unfortunately, the app has stopped
。
为什么应用程序崩溃?这是因为代码在我的LoginActivity
有错误,还是我需要先加载MainActivity
,然后调用LoginActivity
?
您可以使用LoginActivity作为LAUNCHER活动并检查用户是否登录。如果是,启动MainActivity
AndroidManifest.xml:
<activity
android:name=".LoginActivity"
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=".MainActivity"/>
和LoginActivity:
public class LoginActivity extends ActionBarActivity {
private static final String LOGIN_KEY = "LOGIN_KEY";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SharedPreferences pref = getPreferences(Context.MODE_PRIVATE);
if (pref.getBoolean(LOGIN_KEY, false)) {
//has login
startActivity(new Intent(this, MainActivity.class));
//must finish this activity (the login activity will not be shown when click back in main activity)
finish();
}
else {
// Mark login
pref.edit().putBoolean(LOGIN_KEY, true).apply();
// Do something
}
}
}
MainActivity:
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Do something
}
}
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:ignore="GoogleAppIndexingWarning">
<activity android:name=".Activity.MainActivity" />
<activity android:name=".Activity.SignupActivity" />
<activity android:name=".Activity.SigninActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
我认为你需要重新安排一下你的Activity
类。决定应用程序是否首次运行并基于此决定启动一些Activity
是非常简单的。我建议采用以下架构:
你可以设置LauncherActivity
来决定你是否需要启动LoginActivity
或MainActivity
,像这样:
public class LauncherActivity extends Activity {
private boolean firstLaunch = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent i;
SharedPreferences pref = getSharedPreferences(Constants.ApplicationTag, MODE_PRIVATE);
firstLaunch = pref.getBoolean(Constants.FIRST_LAUNCH, true);
if (firstLaunch) {
i = new Intent(LauncherActivity.this, LoginActivity.class);
startActivity(i);
} else {
i = new Intent(LauncherActivity.this, MainActivity.class);
startActivity(i);
}
finish();
}
}
你有另一个问题,我需要整理是在else
语句中调用setContentView
是错误的。您需要将setContentView
放在任何Activity
的super.onCreate(savedInstanceState);
之后。
当你把它放在else
语句中时,内容视图可能没有设置,这会导致应用程序崩溃。
因此,从MainActivity
中删除第一次运行的检查,并将该部分移动到LauncherActivity
中,这将解决问题。
LauncherActivity
的AndroidManifest.xml
可能看起来像这样
<activity
android:name=".Activities.LauncherActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>