【发布时间】:2017-04-25 20:13:35
【问题描述】:
我遇到了一个有趣的问题:我有一个带有 Theme.NoDisplay(无 UI)的启动器类型的 Activity,它应该根据某些条件启动不同的 Activity,即使我正在调用 startActivity(),它也不会如果应用程序是通过启动器图标启动的,则不会启动其中任何一个(它确实启动了LauncherActivity,但随后没有错误/异常而死)。
但是
如果我通过 ADB 启动 LauncherActivity 或向 startActivity() 添加延迟,它似乎工作得很好。
这是一个代码 sn-p。
public class LauncherActivity extends Activity {
private Handler handler = new Handler();
private SharedPreferences preferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
preferences = getSharedPreferences(App.getContext().getString(R.string.preferences_name), MODE_PRIVATE);
int pesel = preferences.getInt(App.getContext().getString(R.string.pref_pesel), 0);
String password = preferences.getString(App.getContext().getString(R.string.pref_password), "");
Intent intent;
if (pesel != 0 && !password.isEmpty()) {
// TODO: server-side password check
intent = new Intent(this, MainActivity.class);
} else {
intent = new Intent(this, RegisterActivity.class);
}
Intent startIntent = getIntent();
intent.setAction(startIntent.getAction());
intent.setFlags(startIntent.getFlags());
if (startIntent.getExtras() != null)
intent.putExtras(startIntent.getExtras());
final Intent readyIntent = intent;
/*
THIS DOENS"T WORK (WORKS IF STARTED VIA ADB THOUGH)
*/
startActivity(readyIntent);
/*
THIS HOWEVER DOES WORK (ALWAYS)
*/
handler.postDelayed(new Runnable() {
@Override
public void run() {
startActivity(readyIntent);
}
}, 5000);
finish();
}
}
【问题讨论】:
-
向我们展示您的清单?
标签: android android-activity activity-lifecycle