Issue
I have implemented Navigation Architecture
Framework to display couple of fragments. Unfortunately each fragment is creating a new activity.
So I started the Activity
with intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
and intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)
flags but they didnt helped much as using them won't show any fragment after showing for the first time only.
nav_graph.xml
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/notification_nav_graph"
app:startDestination="@id/openingNotificationFragment">
<fragment
android:id="@+id/standByScreenFragment"
android:name="com.emaz.android.ui.notification.standby.StandByScreenFragment"
android:label="StandByScreenFragment" />
<fragment
android:id="@+id/welcomeNotificationFragment"
android:name="com.emaz.android.ui.notification.welcome.WelcomeNotificationFragment"
android:label="WelcomeNotificationFragment" />
<fragment
android:id="@+id/openingNotificationFragment"
android:name="com.emaz.android.ui.notification.ingress..OpeningNotificationFragment"
android:label="OpeningFragment" />
<fragment
android:id="@+id/closingNotificationFragment"
android:name="com.emaz.android.ui.notification.ingress..ClosingNotificationFragment"
android:label="ClosingNotificationFragment" />
</navigation>
HomeActivity
Calling NotificationActivity
as
val intent = Intent(this, NotificationActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)
startActivity(intent)
NotificationActivity And calling fragments as
notification_container.visibility = View.GONE
fragment_layout.visibility = View.VISIBLE
val navController = Navigation.findNavController(this, nav_host_fragment.id)
navController.popBackStack()
Log.e("nav", "${navController.currentDestination} and $resId < ResourceId")
navController.navigate(resId, null)
I could see the resId
changing but couldnt see on the screen.
Please help.
Solution
I resolved the multiple activity creation issue. For other people who face same problem. Make sure the app is not in background if you are trying to navigate to fragment using navigation architecture.
Keeping the app active would not create new instance of the activity and app would work as expected.
Answered By - Ahmed S. Durrani
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.