Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling my app from Navigation's deeplink causes my fragment to be instantiad several times

I'm encoutering a very weird problem, when I start the app from the launcher everything is good, but when I start it from a link (in Messenger), the fragment's onCreate/OnCreateView... are called multiple time (but not the activity's)
I have a deep link set to my fragment :

<deepLink
            android:id="@+id/deepLink"
            app:uri="https://www.mywebsite.com/invitation/{groupId}" />

I'm using Android Navigation Component :
app's build :

apply plugin: "androidx.navigation.safeargs"
def nav_version = "2.0.0"
implementation "androidx.navigation:navigation-fragment:$nav_version"
implementation "androidx.navigation:navigation-ui:$nav_version"

Main activity :

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Logger.d("ok that s weird");
        setContentView(R.layout.activity_main);
        Logger.addLogAdapter(new AndroidLogAdapter());
    }
}

My fragment :

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.fragment_welcome, container, false);
        apiService=NetworkingManager.getRetrofit().create(ApiService.class);
        student = WelcomeFragmentArgs.fromBundle(getArguments()).getStudent();
        Logger.d("welcomefragment view being created");
        return v;
    }
        }

My navigation graph : enter image description here

as you can see Fragment perform create is called twice... and in parallel I guess

like image 815
hereForLearing Avatar asked Nov 16 '22 13:11

hereForLearing


1 Answers

New solution:

override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        navController.popBackStack(R.id.home, false)
        setIntent(intent)
        handleDeepLink()
    }

Do the above step in onNewIntent of you MainActivity or where you handle deeplinks.

Not sure if it's good practive @ianhanniballake

like image 56
Sivakumar Chellamuthu Avatar answered Dec 04 '22 14:12

Sivakumar Chellamuthu