user3764893
user3764893

Reputation: 707

How to restore to exact activity after coming from Paused or Resumed state in android

I have two simple activities MainActivity and ThreadActivity. I call ThreadActivity from MainActivity.
The code ofMainActivity:

public class MainActivity extends Activity {
    private Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn = (Button)findViewById(R.id.btn2);
        btn.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 Intent intent = new Intent(MainActivity.this, ThreadActivity.class); 
                 startActivity(intent);
             }
         });
    }
}

And the code of ThreadActivity:

public class ThreadActivity extends Activity{

    private Thread myThread=null;
    Button btn;
    int i = 0;

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

         btn = (Button)findViewById(R.id.btn);
         btn.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                     runThread();
             }
         });
     }

     void  runThread(){
         myThread = new Thread() {
                public void run() {
                    while (i++ < 1000) {
                        try {
                            runOnUiThread(new Runnable() {
                                @Override 
                                public void run() {
                                    btn.setText("#" + i);
                                    Log.d("Thread", "I am running " + i);
                                }
                            });
                            Thread.sleep(300);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                            return;
                        }
                    }
                }
            };
            myThread.start();
     }
}

When I start ThreadActivity I run a simple thread and change button text.

My Problem

The problem is when I press back button, I am being redirected to first activity MainActivity. But instead when back button is being pressed I want my application to exit. In a few words MainActivity should not exist in the stack.

I tried setting android:noHistory="true" for MainActivity but I could not keep the behavior explained in bullet points working. I mean when I pause the application and restore it back, it redirected me to MainActivity instead of ThreadActivity.

Upvotes: 0

Views: 427

Answers (3)

user3764893
user3764893

Reputation: 707

What I had to is set android:noHistory="true" for MainActivity and in the ThreadActivity I had to add the solution mention by @NeTeInStEiN in this quesiotn

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            moveTaskToBack(true);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

Upvotes: 0

FunkTheMonk
FunkTheMonk

Reputation: 10938

From your comments to other peoples' answers, it seems like you want the ThreadActivity to always be resumed instead of the MainActivity when your thread is running.

  1. Do the thread in a Service - the service will mean your application's VM is likely to be kept alive longer. An app with no foreground activities can be killed off quite quickly (even if it has background threads running).
  2. You need to persist that the thread is running, and the progress (if, in the real code that is applicable). Currently you could persist the value of i in your while loop.
  3. Your application's default launcher activity (MainActivity) will launch when you click on it from your launcher. Check if the persisted value has been set, and act as though the user started the ThreadActivity in onCreate, if you finish() in onCreate, the user won't see any UI from the MainActivity
  4. Depending on what you're actually trying to do, you might be able to resume the thread depending on the progress persisted - in this example, you could start from the persisted value of i (instead of 0).

Upvotes: 1

Xaver Kapeller
Xaver Kapeller

Reputation: 49827

Just call finish() when starting the ThreadActivity:

btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent = new Intent(MainActivity.this, ThreadActivity.class);
        startActivity(intent);
        finish();
    }
});

BUT there is a problem with your app. Use a Timer to set the text of the Button! By using a Thread like you do you are creating a memory leak and that is very bad. Try this:

private int i = 0;
private Timer timer;
private final TimerTask timerTask = new TimerTask() {

    @Override
    public void run() {
        btn.post(new Runnable() {
            @Override
            public void run() {
                btn.setText("#" + i++);
            }
        });
    }
};


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

    btn = (Button)findViewById(R.id.btn);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            timer = new Timer();
            timer.schedule(timerTask, 300, 300);
        }
    });
}

@Override
public void onPause() {
    super.onPause();

    if(timer != null) {
        timer.cancel();
    }
}

Upvotes: 1

Related Questions