活动移至开始活动

时间:2012-07-30 18:32:07

标签: android android-intent android-activity

我发现使用停留在后台的应用程序很难启动新活动。 这是我的代码:

public class App1 extends Activity{

    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        registerReceiver(batteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    }

    private BroadcastReceiver batteryInfoReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context c, Intent i) {
            Toast.makeText(getApplicationContext(), "TEST", Toast.LENGTH_LONG).show();
            Intent intent = new Intent("App2.intent.action.Launch");
            intent.putExtra("startedByApp", true);
            startActivity(intent);
        }
    }

    @Override
    public void onBackPressed() {
        moveTaskToBack(true);
    }
}


public class App2 extends Activity{

    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Toast.makeText(getApplicationContext(), "TEST 2", Toast.LENGTH_LONG).show();
    }
}

当App1在前台时,它可以正常工作。 当App1在后台(moveToBack)时,它显示“TEST”,但它没有启动App2(我的屏幕上没有“TEST 2”。

伙计们,你能帮助我吗?

1 个答案:

答案 0 :(得分:1)

您需要一个标记intent.addFlag(Intent.FLAG_ACTIVITY_NEW_TASK);