0

hai i am doing one android app.in middle of the app user click home button that time app is closed then agian user open same app means that time in previous in wich activity i press the home button that page is opened.but i need in my app every time after clicking home button i also need to close the app,then user again open app that time i need open first page every time.i treid using some code but here home button working like back button. if any one having idea suggest me...

     @Override
public void onAttachedToWindow()
{  
    Log.i("TESTE", "onAttachedToWindow");
    this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
    super.onAttachedToWindow();  
}
 public boolean onKeyDown(int keyCode, KeyEvent event) {
     if (keyCode == KeyEvent.KEYCODE_HOME) {
         Log.i("TESTE", "BOTAO HOME");
       
         finish();
        
         return true;
     }
     return super.onKeyDown(keyCode, event);    
     }
Ryan M
  • 18,333
  • 31
  • 67
  • 74
user1105975
  • 121
  • 2
  • 11

2 Answers2

0

Simply override your onResume() method to start the first Activity of your application.

@Override
protected void onResume() {
    super.onResume();
    // Start your first Activity as you would normally do
}

And you need not override your Home button now.


EDIT:

Since onResume() is also called the very first time when an Activity is called, therefore, you need to have a counter to know if the application is running for the first time or is it actually resuming after a pause.

int counter = 1;

@Override
protected void onResume() {
    super.onResume();
    if(counter == 1)
      counter++ ;
    else
      //Start your first Activity as you would normally do
}
Kazekage Gaara
  • 14,972
  • 14
  • 61
  • 108
  • thankou.ur code is some what working.when i click home app closed.but after open app that time app first page is not opend in wich activity i press home button that activity before activity opened not open starting page – user1105975 Jun 07 '12 at 06:48
  • Let me just edit my answer altogether to provide a better solution for you. :-) – Kazekage Gaara Jun 07 '12 at 06:49
  • i have 3 activiteis 1,2,3. in activity1 i set onResume().but i click home button in activity3 that time app closed.after open app that time activity1 not openedmactivity3 is opend – user1105975 Jun 07 '12 at 06:58
  • You need to override `onResume()` for all your activities. :-) and remember to add the part of code that I have not written, starting your first `Activity` using `Intent`. – Kazekage Gaara Jun 07 '12 at 07:00
  • @Override protected void onResume() { Intent i=new Intent(Activity1.this,Activity1.class); startActivity(i); super.onResume(); } sory i didnt get u.in acticity1 i saet above method.and remain acticities i set just onResume().but activity2 is not opend i am getting black sreen... – user1105975 Jun 07 '12 at 07:19
  • Do let me know if it still doesn't help you. :-) – Kazekage Gaara Jun 07 '12 at 09:12
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/12237/discussion-between-user1105975-and-kazekage-gaara) – user1105975 Jun 07 '12 at 09:19
  • I don't see you there. Besides, the above edited code should run fine. – Kazekage Gaara Jun 07 '12 at 09:22
0

whenever you press the home button activity at that time Overwrite the onPause method that activity and when you open the app use your onResume function. Apart if you are willing to start your app from a ceratin point use Preferences variables ,in main activity check the preferences value of to start your app from a specific point.

strike
  • 1,031
  • 8
  • 24
  • override the function on Pause, write the code there whatever action you want to perform like taking a flag variable setting it's value or setting any data in preferences and now check those variable in onResume. – strike Jun 07 '12 at 10:45