Acceptance and rotation of Android intent

I use Intent Filter in my activity to get the URL clicked by the user.

In my onCreate method work, I have the following code

Intent intent = getIntent(); if (Intent.ACTION_VIEW.equals(intent.getAction())) { url = intent.getDataString(); showDialog(DIALOG_ID); } 

It works great except when I rotate my phone. Even if the dialog was closed before the turn, it opens again every time the phone is changed orientation. I can do it.

For your information, I do not want to block the orientation

+4
source share
4 answers

This is a pretty simple fix. In the manifest file, find the activity and add the following:

 android:configChanges="keyboardHidden|orientation" 

This will prevent your logic from repeating in onCreate (or, I assume)

Quote from here :

In some special cases, you can get around restarting your own based on one or more types of configuration changes. This is done with the android: configChanges attribute in its manifest. For anyone, you say that you are dealing with this, you will receive a call to your current onConfigurationChanged activity, and not restarted. If the configuration change is due to the fact that you are not, however, the activity will still be restarted and onConfigurationChanged (Configuration) will not be called.

+2
source

Another solution that does not require self-processing of configuration changes can simply be to check if the savedInstanceState Bundle in onCreate is null before showing the dialog.

If you look at the docs for onCreate, you will see that savedInstanceState will not be null if the action is recreated (due to configuration changes, for example) and thus will be null when the action is launched.

+4
source

Usually you call setIntent(null) to remove the intent used to invoke this operation. However, in practice this does not always work. Apparently, a common workaround is to set the intent action or data, or both, to zero, depending on what you use in the code. In your case, after showing the dialog, I would probably go with intent.setAction(null) .

+2
source

I have never seen this problem until today. I did getIntent().setAction(""); and decided. No more brain damage :)

0
source

All Articles