Reputation: 707
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
ThreadActivity
and the thread is still running.ThreadActivity
.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
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
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.
finish()
in onCreate, the user won't see any UI from the MainActivityUpvotes: 1
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