user1411291
user1411291

Reputation: 81

Activity moved to back starting activity

I find it difficult to start new activity using application staying in the background. Here is my code:

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

When App1 is in the foreground it works fine. When App1 is in the background (moveToBack) it shows "TEST", but it doesn't start App2 (there is no "TEST 2" on my screen.

Guys, can you help me?

Upvotes: 2

Views: 108

Answers (1)

Sunny
Sunny

Reputation: 14808

You need a flag intent.addFlag(Intent.FLAG_ACTIVITY_NEW_TASK);

Upvotes: 1

Related Questions