Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Activity launched from broadcast receiver , keeps in recently tasks in spite of have been finished

I've an issue with two activities that I can't solve:

I have an A activity that programs an alarm with the alarm manager, in 3 seconds the alarm receiver launches a B activity that only has a finish button.

If I program the alarm manager from A and finish this activity pressing the back button, in 3 seconds the B activity appears. All normal. The problem is the following: if I re-open the application by the launcher, the system starts A activity, but if I start the application by long pressing the home button (recently used tasks) I always open the B activity. I need that when I finish B activity , if I reopen the application from anywhere, the A activity should open.

I've tried to add noHistory to activity B, but the problem continues.

Here is the code of A activity:

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

}

B activity:

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>

thanks for your time.

like image 542
Adrian Sanchis Avatar asked Nov 13 '22 21:11

Adrian Sanchis


1 Answers

You wrote:

If I program the alarm manager from A and finish this activity pressing the back button, in 3 seconds the B activity appears. All normal.

Note: You now have 1 running task which has ActivityB as its root activity.

The problem is the following: if I re-open the application by the launcher, the system starts A activity,

Note: When you launch the application from the launcher, this starts another task with ActivityA as its root activity. Now you have 2 running tasks: one with ActivityA as root and one with ActivityB

...but if I start the application by long pressing the home button (recently used tasks) I always open the B activity.

Note: When long-pressing the HOME button and choosing from recently used tasks, you are choosing the task that was started by AlarmReciver which has ActivityB as its root

I need that when I finish B activity , if I reopen the application from anywhere, the A activity should open.

You should be able to get the behaviour you want by setting the following in the <activity> tag for ActivityB:

android:excludeFromRecents="true"

This will ensure that, when you start a task with ActivityB as its root activity, the task will not be shown in the list of recent tasks. In this way, the only way to launch the application will be either from the HOME screen, or by selecting it from the list of recent tasks (which will hold only tasks that were started with ActivityA as the root).

like image 187
David Wasser Avatar answered Nov 15 '22 10:11

David Wasser