Android - Open up Bluetooth setting, but in another view - android

I know that to open up the Bluetooth settings programmatically.
I do something like this:
Intent intentOpenBluetoothSettings = new Intent();
intentOpenBluetoothSettings.setAction(android.provider.Settings.ACTION_BLUETOOTH_SETTINGS);
startActivity(intentOpenBluetoothSettings);
But doing so will take me to the Bluetooth settings page in the same view/application.
How should I go about if I want the Bluetooth settings page to open up in another view/window/page, outside the application?.
The reason why I want this to be done is so that the user will not confuse the settings page and my app.
Thanks.
Update
I tried doing intentOpenBluetoothSettings.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
But no luck in getting it to open in another view.

There are two ways of requesting for enabling Bluetooth:
intentOpenBluetoothSettings.setAction(android.provider.Settings.ACTION_BLUETOOTH_SETTINGS);
or
intentOpenBluetoothSettings.setAction(BluetoothAdapter.ACTION_REQUEST_ENABLE);
If none of them suit your needs, then you might rethink your idea.

I advice reading up a little on android activity lifecycle and backstack.
Settings is NOT running in your app.
But normally everything you start is put on the backstack.
So if you go from your app to some other app (settings) android will remember that.
Your app there is not destroyed but only set on pause (onPause is called).
If you close settings it will return to your app and delete the entry for settings on backstack. I assume that is not the problem here.
But if you don't close settings and then by some means try to start your app again it is not destroyed but only waked up (no onCreate but only onResume). Android will then check that your app has loaded settings currently and settings will show up again.
If you restart your app with an Intent you control (like from a service)
You can:
startMyAppIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
The flag will kill the backstack up to the point where your app is on top of the stack so your app will start no matter if it had started settings before.
Backstack is not cleared completely so if your app was started by another app you may be able to return to that when pressing the back button.

Related

how to restart app when processes doing soft restart?

Edit:
I'm afraid my initial question didn't understood the way I intended so I will rephrase it.
Under some circumstances (permission revoke, memory clean) the OS doing a "soft" restart to the application process. When the user reopen the app the OS will launch the last activity used before the precesses restarted. Is there a way to tell the OS to not try to restore the application's last state and just start from the beginning?
Use this https://github.com/JakeWharton/ProcessPhoenix library. You can customize your restarting using this.
Start the default activity in a new process:
ProcessPhoenix.triggerRebirth(context);
Or, if you want to launch with a specific Intent:
Intent nextIntent = //...
ProcessPhoenix.triggerRebirth(context, nextIntent);
If you want to restart application when that goes to background and agin go to foreground use android:noHistory="true" for certain activities in android Manifest.xml file
the issue is still, that the OS does not notify of such an event - which leaves no other option than to manually handle it. eg. check for the permission not being granted onCreate() and onResume() of the Activity - and than act accordingly, eg. clear the back-stack and then inflate the first one fragment.
the only culprit there is split-screen mode, where onResume() won't be triggered, simply because it never caused an onPause() event. I mean, permission settings on the one side, the application on the other side. to handle this one would have to occasionally check for the state of permissions.

Android completely force quitting an app

I have a login activity. After login, the Main activity is started, which uses fragments for user navigation. I want a button in the nav drawer of my main activity which will completely close the app
Now, I have seen many many threads on this and have tried implementing their solutions. For example:
I have tried finishAffinity() in my Main activity, which should close the current activity as well as all parent activities
I have tried using finish() on the Login activity as soon as I bring up the Main Activity, and then calling finish() again when the user clicks the button
The highest voted answer for this question: Close application and remove from recent apps/, also does not seem work. First, android:autoRemoveFromRecents="true" requires API > 21, but even if I set the minimum SDK version to 21, the app still remains in the list
Finally, I have tried using an Intent when the user clicks the quit button and navigating back to the Login activity, and setting flags with an exit extra, and then finishing the Login activity (i.e. exit android application programmatically)
None of these are working. They will all close the Main Activity, and maybe even close the Login activity. But if the user clicks the app list/current apps/open apps key (the square soft key on most phones), the app is still visible there. When the app is clicked in that list it will take me back to the Login activity screen (I'm unsure if this is starting the app from fresh, or whether its just taking me to the previous Login screen which didn't close)
Out of desperation I have even tried System.exit(0), which I know is bad, but even that doesn't remove the app from the app list
So, how do I programmatically completely quit an app and remove all traces of it being open?
EDIT: I was too hasty in claiming one of the answers below didnt work (see italics above). The answer does remove the app correctly
I think this is the solution you're looking for.
You might consider having another activity named ExitActivity which will be called when you try to exit from your application. The trick here is, the ExitActivity will have android:autoRemoveFromRecents set to true in the manifest file, so that your instance will be cleared automatically from the recents.
Based on my research:
There is no way to force quite an application in android. You can only pause an activity. The discretion to quit an app lies totally with android framework which it does on checking the memory and resource utilization of apps. I could not find the official link for now, but I had read it earlier.
manage it by the OS. bring up the home screen.
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

How to return to already running Android activity

I am working on a fitness app which has a home activity which launches a workouts activity which launches a specific workout activity. In the workout activity, one may start a workout. Thereafter, one might want to then press the Home button and launch a music player or perhaps the web browser. At some point, one would probably launch the app again to return to the already running workout, but that ends up launching a new instance of the app. When I set the launchMode on the home activity to singleTask, it simply goes back to the existing home activity when I tap the launcher icon. What I would like is for it to go back to the workout in progress, which is where you would depart the app.
Essentially, I'm looking for behavior identical to iOS where it would simply restore the app to its current state if you "relaunched" the app and it was still running.
It is supposed to work as you've described. In most cases, it actually does work like that. However, there is a long-standing nasty Android bug which causes the behaviour you've described. This happens when you launch the app for the first time from an IDE (like Eclipse) or by clicking the "open" button on the Installer screen. To see if this is what you're seeing, just do this:
Go to Settings->Applications, choose your app and click "Force close"
Launch your app, do something, press the HOME button
Launch your app again.
You should return to where you left off. If not, something else bad is going on. If that is the case, please post your manifest in your question, because the problem is likely in these.
Don't try to use special launchModes to fix this. This just creates more problems.
See this answer for more information about the nasty long-standing Android bug.

Android: How do I restart my app by activity that I stopped by pressing the home?

When i stop my application pressing home button, i need that the app restart from the same activity when i open it again.
Now my application always start from main activity :(
(i don't know why but on emulator work correctly...)
There are a lot of options for preserving the state of an application - the ost common of these include using the bundle passed in OnCreate to initiate the application correctly.
I suggest you look at the android developers documentation as regards to activity lifespan.
When you begin an Activity, store a SharedPreference that records which Activity you are on. Check this preference when you enter your main Activity and, if set, jump to that Activity.
(If you are just looking for a way to get back to your Activity and don't care about the code, use the Recents menu to get back to your Activity. On devices with dedicated keys, long-press Home.)

How can you easily test the save restore methods in android

I know that if you have to test the onrestoreinstance() the system must kill previously the application. Is there any easy way to test that? I mean not going to open tons of other applications for the system to kill it.
If your AndroidManifest.xml does not block Activity's kill-restore on device rotations, then you can just rotate the device (or emulate device rotation if on emulator).
There is also another cool tool - on the emulator by default you should have 'Dev Tools' app installed. Open it and go to 'Development Settings', then check the 'Don't keep activities' checkbox. Since this moment the activities will be killed by OS as soon as they become invisible (in the background). So pressing 'Home' button will cause a kill for your activity. Then on the home screen pressing your app icon will restore your activity.
You could open your app then kill in the via the home menu.
Device menu
Applications sub menu
Manage Applications
Find application and kill it.
That should help ya out.

Categories

Resources