2017-04-25 85 views
0

我坚持一个有趣的问题:我有一个启动器类型的活动与Theme.NoDisplay(没有用户界面),应该启动不同的活动取决于一些条件,即使我调用startActivity(),如果应用程序是通过启动器图标启动的,它将不会启动它们(它启动LauncherActivity,但随后死亡,没有错误/异常)。无法启动不同的活动从启动器类型活动

无论其

如果我开始通过ADB LauncherActivity或延迟添加到startActivity()它似乎工作得很好。

这是一段代码片段。

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(); 
} 

}

+0

向我们展示您的清单? – t0mm13b

回答

0

我居然找到了解决自己。

我跟随this blog post解释了为什么我们应该拨打finish()之前onResume()Theme.NoDisplay活动。

要解决我的问题,我做了这样的事情,它似乎工作:

@Override 
protected void onDestroy() { 
    super.onDestroy(); 
    if (isFinishing() && intent != null) { 
     startActivity(intent); 
    } 
} 

不幸的是我没有解释清楚为什么,但我会离开这里,反正,可能会帮助别人。