1

Similar to this question, I added no history flag to my login activity. in another side, i have a button in login activity to show wireless setting. When i press back on wireless setting intent, application closed!

How can i have no history flag and prevent application from closing?

Community
  • 1
  • 1
Dr.jacky
  • 3,341
  • 6
  • 53
  • 91
  • just a wild guess here, did you try `startActivityForResult`? Even if you're not reading any result, it probably forces the system to keep your activity around. – Budius Jul 11 '14 at 14:06
  • @Budius Do you believe me my solution(singleTop) does not work anymore now! I don't change any thing! :| Of course i do not want to use singleInstance. it works 100%, but document say "This is a very specialized mode and should only be used in the applications that are implemented entirely as one activity." – Dr.jacky Jul 11 '14 at 14:53
  • @Budius And your solution does not work :( – Dr.jacky Jul 11 '14 at 16:04
  • For some new other people, I don't know if I am out of topic but the link below may hold the answer on this. https://stackoverflow.com/a/7774898/6128863 – Ishimwe Aubain Consolateur Jun 19 '18 at 08:24

2 Answers2

1

When you launch another activity your login activity is finished using the no history flag. You either have to move your wireless setting to another activity or calling finish in the login activity when launching other activity beside wireless setting. In case you keep the wireless button in your login you cannot use no history. You have to set a flag to indicate if setting wireless setting is being launched and in onStop called finish() if this flag is false.

In your Login activity

private boolean mShowSetting;

In onStop()

if (!mShowSetting)
{
    finish();
}

In the method where you start the activity to show setting

mShowSetting = true;

and in your onResume you have to set

mShowSetting = false;
Hoan Nguyen
  • 18,033
  • 3
  • 50
  • 54
  • Move my wireless setting button to where?!Splash screen?! first show a splash, then a login page, and show a alertDialog with no internet connection, turn it on. So you say it can be have no history flag and show some system intent and back to last activity and i should change my scenario?! – Dr.jacky Jul 13 '14 at 14:04
  • No you cannot have no history flag in your login if you keep the wireless button. You have to call finish() in your login onStop() activity if the flag for ShowSetting is false. I edited my answer to make my answer clearer. – Hoan Nguyen Jul 14 '14 at 04:17
  • In your Splash screen you can check for connectivity and if there is no connection show another activity that inform the user that there is no internet connection and a button to go to setting. When this activity comes back after the user pressed the button to go to the setting you can have a loop that check for connection for certain amount of time as the connection does not establish right away and then launch the log in activity. If you implement this way then you can keep the no history flag. – Hoan Nguyen Jul 14 '14 at 04:39
  • When i do this, after show wireless setting and click on back button, application closed! But i want to enter username and password to login. – Dr.jacky Jul 18 '14 at 08:54
  • Do what? You have to be more specific. – Hoan Nguyen Jul 18 '14 at 09:06
  • In login activity if there is no internet connection, when user click on login button, I show a dialog with two button close and turn internet on. With your solution, when user click on turn internet on button, set mShowSetting true, and show wireless setting intent and finish login activity on Stop method. Then on Resume method, set mShowSetting to false. In this case, when user come back from wireless setting to my application to enter username and password to login, there is no application; cause it's finished on Stop method! – Dr.jacky Jul 18 '14 at 09:37
  • Sorry, i forgot to remove nohistory tag from manifest. Thanks a lot. – Dr.jacky Jul 18 '14 at 09:48
0

UPDATE: This solution does NOT work! I don't know why?

Add launchMode to login activity in manifest like this:

<activity android:name="Login" android:noHistory="true" android:launchMode="singleTop" />

If an instance of the activity already exists at the top of the target task, the system routes the intent to that instance through a call to its onNewIntent() method, rather than creating a new instance of the activity.

https://developer.android.com/guide/topics/manifest/activity-element.html

Dr.jacky
  • 3,341
  • 6
  • 53
  • 91