简体   繁体   中英

How I can override onactivityresult method on nested fragment?

I use fragment not this one, import android.app.Fragment

I use this one, import android.support.v4.app.Fragment

because I use it for viewpager

public class PagerAdapter extends FragmentPagerAdapter{

        public PagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {

            if(position == 0){
                return new FragmentOne();
            }else if(position == 1){
                return new FragmentTwo();
            }


        }

        @Override
        public int getCount() {
            return 2;
        }
    }

In that case, FragmentOne extends import android.support.v4.app.Fragment so not error but FragmentTwo extends import android.app.Fragment so it occurs the error

So I have two question

  1. If I use viewpager, Fragment must be extends import android.support.v4.app.Fragment?

  2. If I must be extends import android.support.v4.app.Fragment, how I can use onactivityresult?

You can only use one of both libraries in your app, if you want legacy support add the support library.

And to notify your fragment about activity result changes you can do it using an interface.

In your Activity:

public interface ActivityResultListener {
    onActivityResult(int requestCode, int resultCode, Intent data);
}

And create two methods to set and remove the ActivityResultListener

public void setActivityResultListener(ActivityResultListener listener){
    // You need the listener (ActivityResultListener) variable in the Activity
    this.listener = listener;
}
public void removeActivityResultListener(){
    listener = null;
}

Then, notify your listener if exists when you receive the activity result.

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(listener != null){
        listener.onActivityResult(requestCode, resultCode, data);
    }
}   

Implement the interface in your Fragment:

public class WhateverFragment implements MyActivity.ActivityResultListener{
    // ....
}

This forces you to override the onActivityResult method. There you are going to receive the result when the Activity gets it, do what you want. Only one step left, set your WhateverFragment as listener on the onAttach and remove it on the onDetach methods.

onAttach method:

((MyActivity)getActivity()).setActivityResultListener(this);

onDetach method:

((MyActivity)getActivity()).removeActivityResultListener();

This has been write on the fly so there may be errors.

Ideally you should only use support library's Fragment ie android.support.v4.app.Fragment for backward compatibility.

Since you are using Fragment Adapter which is a class available in Support library and deals only with android.support.v4.app.Fragment hence you must use support library's fragment.

You can work normally with android.support.v4.app.Fragment as you do with android.app.fragment . Support Library also has onActivityResult method and can be easily inherited.

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

For 1st- Use android.support.v4.app.Fragment if your app needs support for api level 15. else if your min api level is 17 you can use android.app.Fragment

For 2nd- In you Activity class-

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case <requestCode>:
               ((FragmentOne)pageAdapter.getItem(0)).onActivityResult(requestCode, resultCode, data);
                break;
        }
    }

FIRST QUESTION

If I use viewpager, Fragment must be extends import android.support.v4.app.Fragment?

In order to use FragmentPagerAdapter or FragmentStatePagerAdapter .

CASE 1 : If you are targeting the app for below 17 android api level then use support library. (Below 17 childFragmentManager is not available in android.app.Fragment)

CASE 2 : If you are targeting the app for 17 and above android api then use android.app.Fragment. (From api level 17 and above childFragmentManager is available android.app.Fragment)

In order to use PagerAdapter

You can choose any, either support library or android.app.Fragment.

SECOND QUESTION

If I must be extends import android.support.v4.app.Fragment, how I can use onactivityresult?

Review this Answer .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM