Skip to content Skip to sidebar Skip to footer

Use Onnewintent(...) In A Fragment

I have a problem. I want to use a the method onNewIntent(...) and getIntent(...). Unfortunately the fragment do not know this intent. How can I use this method in my fragment? T

Solution 1:

You can use a getIntent() with Fragments but you need to call getActivity() first. Something like getActivity().getIntent()... could work.

Solution 2:

I wrote an example code to make an onNewIntent method working. Add an interface class to communicate your fragment activity with UserFragmentGeldaufladen fragment

publicinterfaceOnNewIntentListener {
    voidnewIntent(Intent intent);
}

and in fragment activity, add interface variable and two methods

privateOnNewIntentListener mOnNewIntentListener;

@OverrideprotectedvoidonNewIntent(Intent intent) {
    super.onNewIntent(intent);

    if (mOnNewIntentListener != null) {
        mOnNewIntentListener.newIntent(intent);
    }
}

publicvoidsetOnNewIntentListener(OnNewIntentListener onNewIntentListener) {
    this.mOnNewIntentListener = onNewIntentListener;
}

Now, in UserFragmentGeldaufladen fragment, add onCreate method. When your activity is first time started, you must get intent from activity, like @Dante says

@OverridepublicvoidonCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //get first time intentIntent intent = getActivity().getIntent();

    ((YourActivity)getActivity()).setOnNewIntentListener(newOnNewIntentListener() {
        @OverridepublicvoidnewIntent(Intent intent) {
            //handle your code here for each new intent
        }
    });
}

To receive intent in onNewIntent method, update your activity calling

Intent i = newIntent(context, YourActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
//i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // -> this will be require, if you will be update your fragment from BroadcastReceiver or ServicestartActivity(i);

Hope that help you.

Post a Comment for "Use Onnewintent(...) In A Fragment"