Why my activities are not performing my overridden back button event?
I want to send back my activity to starting one. Let me explain you. I am using Main activity having 4 tabs, and each tabs has his own activity. Each activity in has more activities in it. Problem starts when I click on 1st activity in my 1st tab. The next activity is independent. I create an intent of my next activity. In such case to prevent draw my tab-bar layout in that new activity. I use my existing activity layout, remove all the view in it. Then i put new activity in it.
e.g.. contentViewLayout.removeAllViews(); View view = activityManager.startActivity(id, intent).getDecorView(); contentViewLayout.addView(view, contentViewLayoutParams);
Its done. But problem arise when device BACK button pressed. Because new activity is starting in existing activity. So when back button pressed , then it quite to main activity and application close. I want to do that in such a case "new activity" should be terminate and existing activity must start again. And then if again BACK button pressed then it should terminate main activity. I override the onKeyDown() down method as follow but it. dose not working properly?
In my new activity.
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK){
ch开发者_运维知识库ildActivity = null;
contentViewLayout.removeAllViews();
mainActivity = new Intent(this,MainActivity.class);
View view = activityManager.startActivity(id, intent).getDecorView();
contentViewLayout.addView(view, contentViewLayoutParams);
return false;
}else{
return super.onKeyDown(keyCode, event);
}
}
But it always quite the application instead of closing child activity
I got it! Actually the focused activity was the main activity... not the new activity. So all we have to do to set focused to our new activity. So in onCreate() method of new activity I put focused to one any component in new activit e.g..
button.setFocusable(true); button.requestFocus();
So in such a case new activity onKeyDownButtonWorks
Blockquote
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0 ){
//My code goes here.
return true;
}
return super.onKeyDown(keyCode, event);
}
Take a look at this solution:
ActivityGroup not handling back key for ListActivity
精彩评论