-1

i have this problem: I have lot of layout pages and each has a back button as a picture. That mean when you tap on that picture, you are redirect on layout sreen, which is previous. I have only two activity. That is achieve by this metod:

public void button2 
(View button) {setContentView(R.layout.screen1);}

Is there someone option like this for hardware back button? When i tap on hardware back button, it redirect me on specific layout page? I try option stated on this page: How to handle back button in activity but there not helped. Please can you hepled or explain? Thank you very much.

EDIT:

@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    setRequestedOrientation(ActivityInfoCompat.CONFIG_UI_MODE);    }

@Override
public void onBackPressed() {   
return; }

when use these up, nothing happened.

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

when use these, hardware back button cause return to main screen of the android telephone

Community
  • 1
  • 1
Cosi
  • 21
  • 4
  • Can you show us some code that isn't working? When you say that the link you provided didn't help, what does that mean? What happened when you tried those suggestions? – Bryan Herbst Jun 10 '13 at 19:40
  • @Tanis.7x Sorry, code added. I tried options stated on these link and no one do what i need. Return on previous layout creen. – Cosi Jun 11 '13 at 08:37

1 Answers1

0

You need to actually do something in onBackPressed(). Simply returning will result in exactly what you would expect from a method that does nothing: nothing.

If your Activity is a true second Activity, you can start in onBackPressed with an Intent like so:

@Override
public void onBackPressed() {
    Intent nextActivity = new Intent(this, MyNextActivity.class);
    startActivity(nextActivity);
}

However, it looks like your second "Activity" might just be a different layout for the current Activity. In that case you can simulate a real Activity as so:

@Override
public void onBackPressed() {
    setContentView(R.layout.my_second_activity);
}

I would also recommend sticking with onBackPressed() instead of onKeyDown() unless you are targeting an API version less than 5.

Bryan Herbst
  • 66,602
  • 10
  • 133
  • 120
  • Second option working. But if i have more layouts, exist some condition for recognize, on which one i press hardware button? – Cosi Jun 11 '13 at 14:15
  • I'm not sure I understood that comment. The second option is working, correct? What do you mean by "Because i have two activity with lot of layout with text, exist some condition, which will be doing this?" – Bryan Herbst Jun 11 '13 at 14:17
  • Maybe image can explain better: http://imageshack.us/photo/my-images/5/codem.png/ I have only two activity, for redirecting to loayout using public void. – Cosi Jun 12 '13 at 16:27
  • If you want to keep that design pattern, you can add a "history" of layouts in an [Stack](http://docs.oracle.com/javase/6/docs/api/java/util/Stack.html). Just add the layout ID every time the user selects one and pop a layout ID when back is pressed. However, I would highly encourage you to follow a more standard design pattern and actually create an `Activity` for each of your "activities" – Bryan Herbst Jun 12 '13 at 16:51