活动有viewpager。为什么首先调用片段回调?

时间:2018-01-12 07:03:39

标签: android android-viewpager dagger-2

我有一个活动,其中包含一个填充了api调用结果的片段的viewpager:

ArtistFragment.java

public static ArtistFragment newInstance(String artistName, String imageUrl) {
    ArtistFragment artistFragment = new ArtistFragment();
    Bundle args = new Bundle();
    args.putString(ARTIST_NAME, name);
    args.putString(IMAGE_URL, imageUrl);
    artistFragment.setArguments(args);
    return artistFragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    BaseActivity activity = BaseActivity.get(getActivity());
    AppComponent appComponent = activity.getAppComponent();
    appComponent.inject(this);

    imageUrl = getArguments().getString(IMAGE_URL);
    title = getArguments().getString(TITLE);
}   

MainActivity.java

public class MainActivity extends BaseActivity {

@Override
public void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    appComponent.inject(this);
}

@Override
public void onResume() {
    super.onResume();
    subscriptions.add(client.searchArtists("Impressionist")
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Observer<List<Artist>>() {
                @Override
                public void onCompleted() {

                }

                @Override
                public void onError(Throwable e) {
                    Toast.makeText(getApplicationContext(),
                            e.getMessage(), Toast.LENGTH_LONG)
                            .show();
                }

                @Override
                public void onNext(List<Artist> artists) {
                    ArtistPagerAdapter pagerAdapter = new 
                      ArtistPagerAdapter(getSupportFragmentManager(),
                                         artists);
                    viewPager.setAdapter(pagerAdapter);
                }
            }));
}

BaseActivity.java:

public class BaseActivity extends AppCompatActivity {

    @Inject CompositeSubscription subscriptions;
    AppComponent appComponent;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        appComponent = MyApplication.get(this)
                .getAppComponent();
        appComponent.inject(this);
    }

    public static BaseActivity get(Context context) {
        return (BaseActivity) context;
    }

    public AppComponent getAppComponent() {
        return appComponent;
    }

MyApplication.java

public class MyApplication extends Application {
private AppComponent appComponent;

public static MyApplication get(Context context) {
    return (MyApplication) context.getApplicationContext();
}

@Override
public void onCreate() {
    super.onCreate();
    appComponent = DaggerAppComponent.builder()
            .appModule(new AppModule(this))
            .build();
}

public AppComponent getAppComponent() {
    return appComponent;
}
}

ArtistPagerAdapter:

public class ArtistPagerAdapter extends FragmentStatePagerAdapter {
private List<Artist> results;

public ArtistPagerAdapter(FragmentManager fragmentManager,
                      List<Artist> results) {
    super(fragmentManager);
    this.results = results;
}

@Override
public int getCount() {
    return results.size();
}

@Override
public Fragment getItem(int position) {
    Artist artist = results.get(position);
    String name = artist.getFullName();
    String imageUrl = artist.getImageUrl();
    return ArtistFragment.newInstance(name, imageUrl);
}
}

ApplicationComponent.java

    @Component(modules = AppModule.class)
    @Singleton
    public interface AppComponent {
    void inject(BaseActivity activity);
    void inject(MainActivity activity);
    void inject(ArtistFragment fragment);
}

我在Activity和Fragment onCreate中添加了日志语句。如果我旋转屏幕,为什么片段的onCreate会在活动开始之前调用?

1 个答案:

答案 0 :(得分:0)

将您的代码片段放入

onActivityCreated()

而不是

onCreate()

通常当你的记忆力很低而活动被回收时就会发生这种情况。