Activity launched from the broadcast receiver has been keeping tasks lately, even though they have been completed

I have a problem with two actions that I cannot solve:

I have activity A, which programs the alarm with the alarm manager, after 3 seconds the alarm receiver starts activity B, which has only a completion button.

If I program the alarm manager with A and finish this action by pressing the "Back" button, after 3 seconds the action "B" will appear. Everything is fine. The problem is this: if I re-open the application using start, the system starts action A, but if I launch the application by pressing the home button (recently used tasks), I always open operation “B”. I need that when I complete operation B, if I re-open the application from anywhere, operation A should open.

I tried adding noHistory to activity B, but the problem continues.

Here is action code A:

public class ActivityA extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_a);

    Button b=(Button) findViewById(R.id.initTimer);
    b.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            programTimer();
        }
    });
}

private void programTimer() {
    AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(this, AlarmReciver.class);
    PendingIntent pIntent = PendingIntent.getBroadcast(this, 1, intent,  PendingIntent.FLAG_CANCEL_CURRENT);
    manager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+3000 , pIntent);
}

}

Activity B:

public class ActivityB extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_b);

    Button button= (Button) findViewById(R.id.bFinish);
    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            finish();
        }
    });
}

}

AlarmReciver:

public class AlarmReciver extends android.content.BroadcastReceiver {
private static final String DEBUG_TAG="ReceptorAlarma";
@Override
public void onReceive(Context context, android.content.Intent intent) {     
    //lanzar activity
    Intent i = new Intent(context, ActivityB.class); 
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(i);       
}

}

manifest:

    <application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".ActivityA"
        android:label="@string/title_activity_activity" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity android:name=".ActivityB">            
    </activity>

    <receiver android:name=".AlarmReciver" >
    </receiver>
</application>

thank you for your time.

+5
source share
1 answer

You wrote:

A , 3 B. .

. 1 , ActivityB.

: , A,

.. , ActivityA . : ActivityA root ActivityB

... "" ( ), .

.. HOME , AlarmReciver, ActivityB

, B, , A.

, <activity> ActivityB :

android:excludeFromRecents="true"

, ActivityB . , HOME, ( , ActivityA ).

+1

All Articles