I'm using flutter and I'm a bad understand native android development very well. I'm using 2 Activities in my project:
- SplashActivity - visible 1 time at the time of launching the app.
- MainActivity - the main part of the application.
The problem is that when receiving push-notification from Firebase and when clicking on the notification, the program is completely restarted starting from SlashActivity. I added isTaskRoot to my SplashActivity, but it turned out not to be true, because my notification, when clicked, stopped calling a callback(), only expands the app or opens it starting with SplashActivity.
How it should work:
- If MainActivity already exists (either in the background or foreground), open that same instance.
- If MainActivity was destroyed (the user previously pressed Back), then open the SplashActivity. The splash activity will redirect you itself to MainActivity.
- Notification should be targeted at the MainActivity and changed flow (callback), in depend on the previous points.
@SuppressLint("CustomSplashScreen")
public class SplashActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
// bad decision
if (!isTaskRoot()) {
finish();
return;
}
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.launch_screen);
new Handler().postDelayed(() -> {
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_USER_ACTION);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intent);
overridePendingTransition(0, 0);
finish();
}, 2000);
}
}
import io.flutter.embedding.android.FlutterFragmentActivity
class MainActivity: FlutterFragmentActivity() {
}
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.app">
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:label="example"
android:name="${applicationName}"
android:icon="@mipmap/ic_launcher">
<activity
android:name=".SplashActivity"
android:parentActivityName=".MainActivity"
android:exported="true"
android:allowBackup="true"
android:launchMode="singleTask"
android:theme="@style/AppTheme"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize">
<meta-data
android:name="io.flutter.embedding.android.SplashScreenDrawable"
android:resource="@drawable/launch_background"
/>
<meta-data
android:name="io.flutter.embedding.android.NormalTheme"
android:resource="@style/NormalTheme"
/>
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:exported="true"
android:launchMode="singleTop"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize"
android:theme="@style/LaunchTheme">
<meta-data
android:name="io.flutter.embedding.android.NormalTheme"
android:resource="@style/NormalTheme"
/>
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<intent-filter>
<action android:name="FLUTTER_NOTIFICATION_CLICK" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<meta-data
android:name="flutterEmbedding"
android:value="2" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="high_importance_channel" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/ic_notification" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/colorNotification" />
</application>
<queries>
<intent>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="https" />
</intent>
<intent>
<action android:name="android.intent.action.SENDTO" />
<data android:scheme="mailto" />
</intent>
</queries>
</manifest>
I/flutter (16767): onMessage event: Notification text - Test a body message
E/libc (16767): Access denied finding property "ro.vendor.df.effect.conflict"
E/libc (16767): Access denied finding property "ro.vendor.knock.type"
W/example.app(16767): Accessing hidden method Landroid/os/WorkSource;->add(I)Z (greylist,test-api, reflection, allowed)
W/example.app(16767): Accessing hidden method Landroid/os/WorkSource;->add(ILjava/lang/String;)Z (greylist,test-api, reflection, allowed)
W/example.app(16767): Accessing hidden method Landroid/os/WorkSource;->get(I)I (greylist, reflection, allowed)
W/example.app(16767): Accessing hidden method Landroid/os/WorkSource;->getName(I)Ljava/lang/String; (greylist, reflection, allowed)
W/FirebaseMessaging(16767): Unable to log event: analytics library is missing
W/FirebaseMessaging(16767): Unable to log event: analytics library is missing
D/DecorView[](16767): getWindowModeFromSystem windowmode is 1
D/DecorView(16767): createDecorCaptionView windowingMode:1 mWindowMode 1 isFullscreen: true