onCreate runs 2 times with singletop launch mode set - java

Problem:
My Launching Activity creatively named LoadingActivity's onCreate method is called 2 times.
Manifest Content:
<manifest>
<application>
...
<activity
android:launchMode="singleTop"
android:name="com.awesome.app.activites.LoadingActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
...
</application>
</manifest>
First onCreate call is called with Bundle savedInstanceState which is null, shortly after which the onCreate method is called again, but this time with Bundle savedInstanceState which contains the following serialized content:
Bundle[{com.google.app_measurement.screen_service=Bundle[{referrer_name=LoadingActivity, id=1612091517631091562, name=null}], android:viewHierarchyState=Bundle[{android:views={16908290=android.view.AbsSavedState$1#a24446f, 2131296302=android.view.AbsSavedState$1#a24446f, 2131296313=android.view.AbsSavedState$1#a24446f, 2131296424=android.view.AbsSavedState$1#a24446f, 2131296445=android.widget.ProgressBar$SavedState#b25257c, 2131296581=android.view.AbsSavedState$1#a24446f, 2131296582=android.view.AbsSavedState$1#a24446f}}], androidx.lifecycle.BundlableSavedStateRegistry.key=Bundle[{}], android:lastAutofillId=1073741823, android:fragments=android.app.FragmentManagerState#a24ee05}]
Yes, this doesn't mean much, but it may be related to something at the bottom of the post.
Tried:
I have tried setting launchmode settings with android:launchMode="singleTop" for the LoadingActivty in my AndroidManifest.xml, but this does not help at all, it still runs the onCreate 2 times.
I have gone through my onCreate method, no where do I onCreate again or change ApplicationContext settings i.e. Orientation (physically or programmatically), etc.
Any idea why this happens and what I can do to resolve it.
For what it is worth, I have recently updated my deps and see this in my Manifest:
It compiles (change the package name for obv reasons) with the portrait launchmode, but I think this may have something to do with the double launch

Related

How do you change the default dialer package in android studio?

I'm currently trying to change the default dialer package of the machine to my application. When running the code that is supposed to change it, however, it results in the default dialer package retaining a value of NULL. This is loosely following the guide here, but some of it is deprecated so I've had to make some adjustments.
EDIT: has REQUEST_CODE_DEFAULT_DIALER been changed somehow? I can't find it referenced anywhere other than outdated tutorials on how to do this, so I'm not sure what it would even have been changed to. I'm getting a "cannot resolve symbol" error whenever I try to use it.
I've included the following under the relevant activity in my AndroidManifest.xml:
<intent-filter>
<action android:name="android.intent.action.DIAL" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.DIAL" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme = "tel"/>
</intent-filter>
The following code executes the package change and then prints its value to logcat so I can see what happens:
Log.i("Before", "Before default dialer change");
startActivity(new Intent(TelecomManager.ACTION_CHANGE_DEFAULT_DIALER).putExtra(TelecomManager.EXTRA_CHANGE_DEFAULT_DIALER_PACKAGE_NAME,getPackageName()));
TelecomManager telecomManager = ((TelecomManager) getSystemService(Context.TELECOM_SERVICE));
if(telecomManager.getDefaultDialerPackage() == null) {
Log.i("Default dialer package:", "NULL");
} else {
Log.i("Default Dialer Package:", telecomManager.getDefaultDialerPackage());
}
I'm getting "Default dialer package:: NULL" in logcat every time I try this.
Intents are asynchronous. If you start an activity to do something, you have to wait for that intent to finish before using the results of it. You're instead immediately querying for the default dialer package. That will always fail, because it wouldn't even have shown the picker on screen yet.

Android not detecting when device rotated

I am trying to start a new activity when an Android device is rotated, but I don;t even seem to be detecting the rotation in the emulator.
I've read the thread at Android: listen for Orientation change? and that all seems to amke sense, but its just not working.
In my manifest I have:
<activity
android:name=".MainActivity"
android:configChanges="orientation|screenSize"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
and in my mainActivity.java I have:
#Override
public void onConfigurationChanged (Configuration newConfig) {
super.onConfigurationChanged(newConfig);
int orientation=newConfig.orientation;
switch(orientation) {
case Configuration.ORIENTATION_LANDSCAPE:
showMessage("landscape");
break;
case Configuration.ORIENTATION_PORTRAIT:
showMessage("portrait");
break;
}
}
This obviously won't start the new Activity, but I am trying to get the orientation detection working first (showMessage just calls a Toast and is working elsewhere in my code, so that's not why I am not seeing anything).
When I run this in the emulator and use the rotate buttons, the emulator rotates as expected but I never see the Toast...
Where am I going wrong? (I am importing android.content.res.Configuration as required for the Configuration constants).
onConfigurationChanged method will not be invoked when you rotate your device, actually, nothing will be invoked because of this line:
android:screenOrientation="portrait"
device is just locked in a portrait mode, remove this line, and onConfigurationChanged method should be invoked.
If you want to detect that device is rotated and keep android:screenOrientation="portrait" line you can use accelerometer sensor.
Try after removing this lines
android:configChanges="orientation|screenSize"
android:screenOrientation="portrait"
because you are locking screen orientation in manifest and you are
also overriding on config change in activity.

Android createChooser appear in front of Activity

I'm hoping one of you fine folk may be able to help me out with a little issue I am having whilst using Android Intent.createChooser(). I am learning as I go with my application so I apologise if I have the wrong end of the stick with how this should work!
I have been following a number of tutorials and have implemented some code which starts an Intent.createChooser() to share a picture to social media. The code that I have used works fine in that I am able to share the picture as I intended, my issue is that when the code is run my main activity (or any activity I call the code from) appears to close and the chooser appears in front of the homescreen or any other applications I may have open. The effect that I was hoping to have, and the one which seemed to be apparent in the tutorials, is for the chooser to appear in front of the running activity (eg as it does for the gallery).
I am using LibGDX meaning that I have had to use an interface to call the android function that executes the chooser code (in my AndroidLauncher java file). The interfaces all work fine but I wasn't sure if that may have a bearing on the way in which this code operates. I have also played around with swapping to a new activity to implement the chooser code, which I had moved from the AndroidLauncher. This had the same end result.
Any advice on how to make the chooser appear in front of my application would be greatly appreciated! The code which implements the createChooser is included below:
public void ShareHandler(){
String type = "image/*";
String mediaPath = Environment.getExternalStorageDirectory().getPath()+"/Smile.png";
createShareIntent(type, mediaPath);
}
private void createShareIntent(String type, String mediaPath)
{
Intent share = new Intent(Intent.ACTION_SEND);
share.setType(type);
File media = new File(mediaPath);
Uri uri = Uri.fromFile(media);
share.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(share, "Share to"));
}
Android Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.game"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="25" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/GdxTheme">
<activity
android:name=".AndroidLauncher"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
android:label="#string/app_name"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".FacebookServiceHandler" />
<meta-data
android:name="come.facebook.sdk.ApplicationID"
android:value="#string/facebook_app_id" />
<provider
android:name="com.facebook.FacebookContentProvider"
android:authorities="com.facebook.app.FacebookContentProviderxxxxxxxxxxxxx"
android:exported="true" />
</application>
</manifest>
Thanks in advance, Ben.
If
android:name=".FacebookServiceHandler"
is the activity, that handle the social media sharing of the picture, i would expand that to something like this:
<activity
android:name=".FacebookServiceHandler">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".AndroidLauncher">
</meta-data>
</activity>
This will tell the programm, that the AndroidLauncher is the parentactivity and should make it appear in front of your activity.
Thank you for the suggestions, I have now solved my issue. It turned out that my code was working correctly all along and the issue I was experiencing was as a result of an exit statement within my Core (LibGDX) java code. Essentially when the startActivity(Intent.createChooser(...)) was called, a dispose function within my core files was also being called. This dispose function contained the exit command and was causing the application to finish.

How to find whether the app is called by "Share-Via"?

I have added my Android app to the "Share-Via" window. Following code is added to the onCreate() method of my code.
if(getIntent().getAction().equals(Intent.ACTION_SEND))
{
String text = getIntent().getStringExtra(Intent.EXTRA_TEXT);
textField.setText(text);
}
Now the issues is, if this app activity get called from the share-via, it works. But if it get called directly (user opens the app -> go to that activity) this crashes with NullPointerException. I am getting NullPointerException right in here
if(getIntent().getAction().equals(Intent.ACTION_SEND))
Following is how this Share-Via configured in the manifest file.
<activity
android:name="com.xx.xx.xx"
android:screenOrientation="portrait"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain"/>
</intent-filter>
</activity>
What is the issue here?
Either getIntent() or getAction() is returning null.
You need to check for that.

Confusion over execution order

I have a game application made up of several different activities. The first to be called is a splash screen, when this completes, this finishes and starts up another activity via an intent. In order to have access to some global data that is consistent across all the activities, I also have a "globals" class like this:
public class Globals extends Application
{
int global_variable_A;
int global_variable_B;
int global_variable_C;
public void onCreate()
{
// stuff
}
}
In the androidmanifest.xml I have the the following (amongst other things):
<application
android:icon="#drawable/mygame_icon"
android:screenOrientation="portrait"
android:label='"My Game"' android:name=".Globals">
<activity
android:label="My Game"
android:name=".Splash"
android:screenOrientation="portrait">
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
My question now is, which will be executed first, the onCreate of Globals or the onCreate of Splash? Or are they run at the same time on different threads? I ask because I'm getting some inconsistent behaviour that would be explained if they were on different threads.
onCreate() Global off course.. Application gets executed first and then the Activity,,.. you can test for yourself by keeping a debug point in Application onCreate() method..

Categories

Resources