0

In my application when i click home button and when again when i open the app it should come from first.I am having splash screen as my first activity.when i open the app,the app should start from splash screen.

 @Override public void onAttachedToWindow() { super.onAttachedToWindow(); this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if(keyCode == KeyEvent.KEYCODE_HOME) { Log.i("Home Button","Clicked"); onPause(); } return false; } protected void onPause() { super.onPause(); Intent i=new Intent(H2.this,HomeexampleActivity.class); startActivity(i); // Start your first Activity as you would normally do } 

I tried in this way by overriding on pause method but it is not working.please help me how to solve this issue

4
  • Inside onPause(), super.onPause() should be the first statement. Commented Jun 12, 2012 at 7:10
  • i tried in that way also but i didnt get Commented Jun 12, 2012 at 7:11
  • That was not a solution to your problem. That is how it should be. Keep it that way. Commented Jun 12, 2012 at 7:13
  • try finishing this current activty as if(!isFinishing()&&<your_otehr_bolelan_indefify_home_presse>){ finish(); } Commented Jun 12, 2012 at 7:36

4 Answers 4

1

You can try this:

 Boolean hasGone = false; @Override void onResume() { super.onResume(); if(hasGone){ Intent intent = new Intent(H2.this, HomeexampleActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); } } @Override void onPause() { super.onPause(); hasGone = true; } 
Sign up to request clarification or add additional context in comments.

5 Comments

thanks for your resopnse it is working fine for home button but if i click escape it is not going to previous page instead it is coming from first page that is splash screen in my application.
@user1083266: I had test my code, and it worked fine when I click back key (like escape). Have you removed onKeyDown( that call onPause()) from above code yet?
Very strange, because I tested this code. when you in H2 activity, press back it go to HomeexampleActivity? can you update the order called activity?
Splashscree->homeactivity->h1->h2 this is my order of clases
@user1083266: I don' know where is your problem because this code worked on my emulator. Maybe you should update full source code of H2 activity in your question, then I may help.
1

Try writing this line in your each activity tag in Manifest file

android:launchMode="singleTask" 

Comments

0

use i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); before startActivity(i);

2 Comments

if i use on pause method am having on button on click those actions are not working
i tried using on stop also but home button is working like back button please suggest me how to solve this issue
0

You should have that code in onResume() not in onPause(). Because, when you click on your Home button, onPause() is called. But when you resume your application, onResume() is called. Hence, if you want to redirect your application to start from the beginning again, start your first Activity in onResume() method in all your Activity classes.

Keep in mind, that onResume() is also called once immediately after onCreate(), so keep a flag value to check whether your onResume() is called after onCreate() or is it called when your application is resuming.

7 Comments

so no need of on pause method
I believe no. Because, as I said, onPause() is called when you pause your Activity. But what you want to do is something that has to be done when you resume your Activity. Hence, onResume() is the way to go.
i am having button on click it not going to next page
Did you do the flag check I told you to? remember, onResume() is also called after onCreate(). Probably why you are having trouble I guess.
in my application i am having three activities home1,h1,h2.the button which is in home1 when i click it should go to h1 but due to on resume it is not going to h1
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.