alex
alex

Reputation: 300

Android task history stack: can have "duplicate activities"?

I would like to have this behavior in a task history stack:

This way, if user presses back button, he goes back to B, and pressing back button again goes back to A (always same instance of A).

Using intent flag "FLAG_ACTIVITY_REORDER_TO_FRONT" I have achieved the behavior:

So same instance of A is actually brought to front, but after leaving B with back button, A is no longer between B and Main, so Main is shown.

  1. Is there any flag/activity-attribute or so, that can simplify achieving this behavior?
  2. Or do I need to handle "back button presses" on activity A?
    • If so, assuming I am on scenario 2.2, how can I reorder A (after detecting back-button) to put it in between Main and B?
  3. Any other comments/suggestions will be appreciated.

Thank you!

Upvotes: 1

Views: 1584

Answers (2)

Igor
Igor

Reputation: 5

You can try using finish(); right after you start your new activity:

startActivity(intent);
finish();

Upvotes: 0

hasanghaforian
hasanghaforian

Reputation: 14022

I think that your desired scenario is normally impossible.Because of Tasks and Back Stack says:

When the user presses the Back button, the current activity is popped from the top of the stack (the activity is destroyed)

So if your task be A -> B -> A (duplicate) and user presses back button, he goes back to B and A is destroyed and he can not go back to A.
I wrote tree activities A,A1,A2 that have this behavior:
Main -> A -> A1 -> A2 -> A1(duplicated)
Next when user presses BACK in A2:
A1 -> A2 -> A1(same) -> A -> Main
This is their codes:
Activity A is:

package t.t;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class TaskTestActivity extends Activity {
    Button btn; 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main0);
        btn = (Button) findViewById(R.id.button1);
        btn.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent i = new Intent(TaskTestActivity.this, TaskTestActivity1.class);
                i.putExtra("loader", "A");
                TaskTestActivity.this.startActivity(i);
            }
        });
    }
}    

Activity A1 is:

package t.t;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;

public class TaskTestActivity1 extends Activity {
    Button btn;
    String str = "";
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main1);
        btn = (Button) findViewById(R.id.button1);
        btn.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent i = new Intent(TaskTestActivity1.this, TaskTestActivity2.class);
                i.putExtra("loader", "A1");
                TaskTestActivity1.this.startActivity(i);
            }
        });
    }
    @Override
    public void onBackPressed() {
        System.out.println(str);
        if(str.equals("ali")){
            Intent i = new Intent(this,TaskTestActivity2.class);
            this.startActivity(i);
            str = "";
            System.out.println("BACK");
        }else{
            Intent i = new Intent(this,TaskTestActivity.class);
            this.startActivity(i);
            str = "";
            System.out.println("BACK1");
        }

    }
    @Override
    protected void onNewIntent(Intent intent) {
        // TODO Auto-generated method stub
        super.onNewIntent(intent);
        str = "ali";
    }

}   

Activity A2 is:

package t.t;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;

public class TaskTestActivity2 extends Activity {
    Button btn;
    int i = 0;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btn = (Button) findViewById(R.id.button1);
        btn.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent i = new Intent(TaskTestActivity2.this,
                        TaskTestActivity1.class);
                i.putExtra("loader", "A2");
                TaskTestActivity2.this.startActivity(i);
            }
        });

    }
}    

Note that I override onBackPressed() in activity A1 and you would to detect you want to back to A2 or A,So I Add an extra to intent and override onNewIntent(Intent intent) in A1.My project manifest is:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="t.t"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" android:hardwareAccelerated="true">
        <activity
            android:label="A"
            android:launchMode="standard"
            android:name=".TaskTestActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:label="A2"
            android:launchMode="singleInstance"
            android:name=".TaskTestActivity2" >
        </activity>
        <activity android:name="TaskTestActivity1" android:launchMode="singleTask" android:label="A1"></activity>
    </application>

</manifest>   

Pay attention to "singleInstance" and "singleTask" properties in Activities elements.Finally you can use this layouts for your activities to sure A1 is duplicated:

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="A2"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <RatingBar
        android:id="@+id/ratingBar1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

main1.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="A1"
        android:textAppearance="?android:attr/textAppearanceLarge" />





    <RatingBar
        android:id="@+id/ratingBar1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

main0.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >



    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="A"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>

Upvotes: 2

Related Questions