【问题标题】:savedInstanceState empty after onLoadReset but works well if onCreate is called and onLoadReset is not(like in case of device rotation)saveInstanceState 在 onLoadReset 之后为空,但如果 onCreate 被调用并且 onLoadReset 没有被调用(如设备旋转),则效果很好
【发布时间】:2016-12-07 11:32:23
【问题描述】:

我有一个片段,我在其中使用 AsyncTaskLoader 从 API 获取数据,然后使用自定义适配器对两个单独的 RecyclerView 进行膨胀。问题是,当我单击回收器视图进入该特定位置的 detailsActivity 然后返回原始 Activity 时,我发现 saveInstanceState 为空。但如果我确实旋转我的设备,它工作得很好。我不认为这是正确的行为。如果它适用于屏幕旋转,那么它也应该适用于我遇到问题的情况。

这是我的片段的代码:

public class MoviePosterFragment extends Fragment implements LoaderManager.LoaderCallbacks<ArrayList<Movie>> {

private static final int POPULAR_MOVIE_LOADER_ID = 1111;
private static final int UPCOMING_MOVIE_LOADER_ID = 9999;
ArrayList<Movie> popularMovies;
ArrayList<Movie> upcomingMovies;
PopularMoviesAdapter mPopularMoviesAdapter;
UpcomingMovieAdapter mUpcomingMovieAdapter;
RecyclerView mPopularMovieRecyclerView;
RecyclerView mUpcomingMovieRecyclerView;
Uri.Builder uriBuilder;
String savedInstance;
LinearLayoutManager layoutManagerPopularMoviesPoster;
LinearLayoutManager layoutManagerUpcomingMoviesPoster;

public MoviePosterFragment() {
    // Required empty public constructor
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.v("############******", "onCreate called");
    if (savedInstanceState == null) {
        popularMovies = new ArrayList<>();
        upcomingMovies = new ArrayList<>();
        savedInstance = "empty";
        Log.v("############******", "onCreate savedInstance is " + savedInstance);
        //First of all check if network is connected or not then only start the loader
        ConnectivityManager connMgr = (ConnectivityManager)
                getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
        if (networkInfo != null && networkInfo.isConnected()) {
            /*
             *fetch data. Get a reference to the LoaderManager, in order to interact with loaders.
            */
            Log.v("############******", "startPopularMoviesLoaderManager called");
            startPopularMoviesLoaderManager();
            Log.v("############******", "startUpcomingMoviesLoaderManager called");
            startUpcomingMoviesLoaderManager();

        }

    } else {
        savedInstance = "not empty";
        Log.v("############******", "onCreate savedInstance is " + savedInstance);
        popularMovies = savedInstanceState.getParcelableArrayList("popularMovies");
        upcomingMovies = savedInstanceState.getParcelableArrayList("upcomingMovies");

    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    Log.v("############******", "onCreateView savedInstance is " + savedInstanceState);
    // Inflate the layout for this fragment
    View rootView = inflater.inflate(R.layout.fragment_movie_posters, container, false);

    /* Code referenced from the @link:
     * "https://guides.codepath.com/android/using-the-recyclerview"
     */
    /*
     * Lookup the recyclerView in activity layout
     */
    mPopularMovieRecyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerViewPopularMovies);
    mUpcomingMovieRecyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerViewTopMoviesMovies);

     /*
      * Setup layout manager for items with orientation
      * Also supports `LinearLayoutManager.HORIZONTAL`
      */
    layoutManagerPopularMoviesPoster = new LinearLayoutManager(getActivity(),
            LinearLayoutManager.HORIZONTAL, false);
        /* Optionally customize the position you want to default scroll to */
    layoutManagerPopularMoviesPoster.scrollToPosition(0);
        /* Attach layout manager to the RecyclerView */
    mPopularMovieRecyclerView.setLayoutManager(layoutManagerPopularMoviesPoster);

     /*
      * Setup layout manager for items with orientation
      * Also supports `LinearLayoutManager.HORIZONTAL`
      */
    layoutManagerUpcomingMoviesPoster = new LinearLayoutManager(getActivity(),
            LinearLayoutManager.HORIZONTAL, false);
        /* Optionally customize the position you want to default scroll to */
    layoutManagerUpcomingMoviesPoster.scrollToPosition(0);
        /* Attach layout manager to the RecyclerView */
    mUpcomingMovieRecyclerView.setLayoutManager(layoutManagerUpcomingMoviesPoster);

    SnapHelper snapHelperForPopularMovieRecyclerView = new GravitySnapHelper(Gravity.START);
    snapHelperForPopularMovieRecyclerView.attachToRecyclerView(mPopularMovieRecyclerView);

    SnapHelper snapHelperForUpcomingMovieRecyclerView = new GravitySnapHelper(Gravity.START);
    snapHelperForUpcomingMovieRecyclerView.attachToRecyclerView(mUpcomingMovieRecyclerView);

    /* Code referenced from the @link:
    * "https://guides.codepath.com/android/using-the-recyclerview"
    */

    // Create mPopularMoviesAdapter passing in the sample user data
    mPopularMoviesAdapter = new PopularMoviesAdapter(getActivity(), popularMovies);
    mPopularMoviesAdapter.setMovieData(popularMovies);
    // Attach the mPopularMoviesAdapter to the recyclerview to populate items
    mPopularMovieRecyclerView.setAdapter(mPopularMoviesAdapter);

    // Create mUpcomingMoviesAdapter passing in the sample user data
    mUpcomingMovieAdapter = new UpcomingMovieAdapter(getActivity(), upcomingMovies);
    mUpcomingMovieAdapter.setMovieData(upcomingMovies);
    // Attach the mUpcomingMoviesAdapter to the recyclerview to populate items
    mUpcomingMovieRecyclerView.setAdapter(mUpcomingMovieAdapter);

    return rootView;
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    Log.v("############******", "onActivityCreated called and saveInstance is " + savedInstanceState);
    mPopularMoviesAdapter = new PopularMoviesAdapter(getActivity(), popularMovies);
    mUpcomingMovieAdapter = new UpcomingMovieAdapter(getActivity(), upcomingMovies);
    mUpcomingMovieAdapter.setMovieData(upcomingMovies);
    mPopularMoviesAdapter.setMovieData(popularMovies);
    mPopularMovieRecyclerView.setAdapter(mPopularMoviesAdapter);
    mUpcomingMovieRecyclerView.setAdapter(mUpcomingMovieAdapter);
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    Log.v("############******", "onSaveInstanceState called");
    outState.putParcelableArrayList("popularMovies", popularMovies);
    outState.putParcelableArrayList("upcomingMovies", upcomingMovies);
}


private void startPopularMoviesLoaderManager() {
    LoaderManager loaderManager = getLoaderManager();
    Log.v("############******", "initLoader called with id " + POPULAR_MOVIE_LOADER_ID);
    loaderManager.initLoader(POPULAR_MOVIE_LOADER_ID, null, this);
    Log.v("############******", "startPopularMoviesLoaderManager finished");
}

private void startUpcomingMoviesLoaderManager() {
    LoaderManager loaderManager = getLoaderManager();
    Log.v("############******", "initLoader called with id " + UPCOMING_MOVIE_LOADER_ID);
    loaderManager.initLoader(UPCOMING_MOVIE_LOADER_ID, null, this);
    Log.v("############******", "startUpcomingMoviesLoaderManager finished");
}

@Override
public Loader<ArrayList<Movie>> onCreateLoader(int id, Bundle args) {
    if (id == POPULAR_MOVIE_LOADER_ID) {
        Log.v("############******", "onCreateLoader called with id " + POPULAR_MOVIE_LOADER_ID);
        Uri baseUri = Uri.parse(UrlsAndConstants.MoviePosterQuery.DEFAULT_URL);
        Log.v("############", "baseUri is " + baseUri.toString());
        uriBuilder = baseUri.buildUpon();
        Log.v("############", "uriBuilder is " + uriBuilder.toString());
        uriBuilder.appendQueryParameter(API_KEY_PARAM, API_KEY_PARAM_VALUE);
        Log.v("############", "uriBuilder.toString() is " + uriBuilder.toString());
        uriBuilder.appendQueryParameter(SORT_BY_KEY, SORT_BY_POPULARITY_VALUE_DESCENDING);

    } else if (id == UPCOMING_MOVIE_LOADER_ID) {
        Log.v("############", "onCreateLoader called with id " + UPCOMING_MOVIE_LOADER_ID);
        Uri baseUri = Uri.parse("https://api.themoviedb.org/3/movie/upcoming");
        Log.v("############", "baseUri is " + baseUri.toString());
        uriBuilder = baseUri.buildUpon();
        Log.v("############", "uriBuilder is " + uriBuilder.toString());
        uriBuilder.appendQueryParameter(API_KEY_PARAM, API_KEY_PARAM_VALUE);
        Log.v("############", "uriBuilder.toString() is " + uriBuilder.toString());
    }
    return new MoviePosterLoader(getActivity().getApplicationContext(), uriBuilder.toString());
}

@Override
public void onLoadFinished(Loader<ArrayList<Movie>> loader, ArrayList<Movie> incomingMovieArrayList) {
    switch (loader.getId()) {
        case POPULAR_MOVIE_LOADER_ID:
            Log.v("############******", "onLoadFinished called with id " + POPULAR_MOVIE_LOADER_ID);
            if (incomingMovieArrayList.isEmpty()) {
                Log.v("******************", "popularMovies isEmpty");
                return;
            } else {
                popularMovies = incomingMovieArrayList;
                mPopularMoviesAdapter = new PopularMoviesAdapter(getActivity(), popularMovies);
                mPopularMoviesAdapter.setMovieData(popularMovies);
                mPopularMovieRecyclerView.setAdapter(mPopularMoviesAdapter);

            }
            break;
        case UPCOMING_MOVIE_LOADER_ID:
            Log.v("############******", "onLoadFinished called with id " + UPCOMING_MOVIE_LOADER_ID);
            if (incomingMovieArrayList.isEmpty()) {
                Log.v("******************", "popularMovies isEmpty");
                return;
            } else {
                upcomingMovies = incomingMovieArrayList;
                mUpcomingMovieAdapter = new UpcomingMovieAdapter(getActivity(), upcomingMovies);
                mUpcomingMovieAdapter.setMovieData(upcomingMovies);
                mUpcomingMovieRecyclerView.setAdapter(mUpcomingMovieAdapter);
            }
            break;
    }
}

@Override
public void onLoaderReset(Loader<ArrayList<Movie>> loader) {
    Log.v("############******", "onLoaderReset called ");

}

}

而且正如您从下面的 Logcat 消息中看到的那样,问题确实存在。

Logcat 消息:

`首次启动应用时:

12-07 01:37:56.450 9756-9756/me.abhishekraj.showmyshow V/############******: onCreate called
12-07 01:37:56.450 9756-9756/me.abhishekraj.showmyshow V/############******: onCreate savedInstance is empty
12-07 01:37:56.450 9756-9756/me.abhishekraj.showmyshow V/############******: startPopularMoviesLoaderManager called
12-07 01:37:56.453 9756-9756/me.abhishekraj.showmyshow V/############******: initLoader called with id 1111
12-07 01:37:56.453 9756-9756/me.abhishekraj.showmyshow V/############******: onCreateLoader called with id 1111
12-07 01:37:56.455 9756-9756/me.abhishekraj.showmyshow V/############******: startPopularMoviesLoaderManager finished
12-07 01:37:56.455 9756-9756/me.abhishekraj.showmyshow V/############******: startUpcomingMoviesLoaderManager called
12-07 01:37:56.455 9756-9756/me.abhishekraj.showmyshow V/############******: initLoader called with id 9999
12-07 01:37:56.455 9756-9756/me.abhishekraj.showmyshow V/############******: startUpcomingMoviesLoaderManager finished
12-07 01:37:56.456 9756-9756/me.abhishekraj.showmyshow V/############******: onCreateView savedInstance is null
12-07 01:37:56.493 9756-9756/me.abhishekraj.showmyshow V/############******: onActivityCreated called and saveInstance is null
12-07 01:37:57.683 9756-9756/me.abhishekraj.showmyshow V/############******: onLoadFinished called with id 9999
12-07 01:37:57.731 9756-9756/me.abhishekraj.showmyshow V/############******: onLoadFinished called with id 1111
After a popular movie poster is clicked
12-07 01:29:16.062 2256-2256/me.abhishekraj.showmyshow V/############******: onSaveInstanceState called
After coming back from DetailActivity
12-07 01:38:52.785 9756-9756/me.abhishekraj.showmyshow V/############******: onLoaderReset called
12-07 01:38:52.785 9756-9756/me.abhishekraj.showmyshow V/############******: onLoaderReset called
12-07 01:38:52.851 9756-9756/me.abhishekraj.showmyshow V/############******: onCreate called
12-07 01:38:52.851 9756-9756/me.abhishekraj.showmyshow V/############******: onCreate savedInstance is empty
12-07 01:38:52.851 9756-9756/me.abhishekraj.showmyshow V/############******: startPopularMoviesLoaderManager called
12-07 01:38:52.851 9756-9756/me.abhishekraj.showmyshow V/############******: initLoader called with id 1111
12-07 01:38:52.851 9756-9756/me.abhishekraj.showmyshow V/############******: onCreateLoader called with id 1111
12-07 01:38:52.851 9756-9756/me.abhishekraj.showmyshow V/############******: startPopularMoviesLoaderManager finished
12-07 01:38:52.851 9756-9756/me.abhishekraj.showmyshow V/############******: startUpcomingMoviesLoaderManager called
12-07 01:38:52.851 9756-9756/me.abhishekraj.showmyshow V/############******: initLoader called with id 9999
12-07 01:38:52.851 9756-9756/me.abhishekraj.showmyshow V/############******: startUpcomingMoviesLoaderManager finished
12-07 01:38:52.851 9756-9756/me.abhishekraj.showmyshow V/############******: onCreateView savedInstance is null
12-07 01:38:52.855 9756-9756/me.abhishekraj.showmyshow V/############******: onActivityCreated called and saveInstance is null
12-07 01:38:53.326 9756-9756/me.abhishekraj.showmyshow V/############******: onLoadFinished called with id 9999
12-07 01:38:54.033 9756-9756/me.abhishekraj.showmyshow V/############******: onLoadFinished called with id 1111
After a upcoming movie poster is clicked
12-07 01:30:04.572 2256-2256/me.abhishekraj.showmyshow V/############******: onSaveInstanceState called
After coming back from DetailActivity
12-07 01:39:51.672 9756-9756/me.abhishekraj.showmyshow V/############******: onLoaderReset called
12-07 01:39:51.672 9756-9756/me.abhishekraj.showmyshow V/############******: onLoaderReset called
12-07 01:39:51.759 9756-9756/me.abhishekraj.showmyshow V/############******: onCreate called
12-07 01:39:51.759 9756-9756/me.abhishekraj.showmyshow V/############******: onCreate savedInstance is empty
12-07 01:39:51.759 9756-9756/me.abhishekraj.showmyshow V/############******: startPopularMoviesLoaderManager called
12-07 01:39:51.759 9756-9756/me.abhishekraj.showmyshow V/############******: initLoader called with id 1111
12-07 01:39:51.759 9756-9756/me.abhishekraj.showmyshow V/############******: onCreateLoader called with id 1111
12-07 01:39:51.760 9756-9756/me.abhishekraj.showmyshow V/############******: startPopularMoviesLoaderManager finished
12-07 01:39:51.760 9756-9756/me.abhishekraj.showmyshow V/############******: startUpcomingMoviesLoaderManager called
12-07 01:39:51.760 9756-9756/me.abhishekraj.showmyshow V/############******: initLoader called with id 9999
12-07 01:39:51.760 9756-9756/me.abhishekraj.showmyshow V/############******: startUpcomingMoviesLoaderManager finished
12-07 01:39:51.761 9756-9756/me.abhishekraj.showmyshow V/############******: onCreateView savedInstance is null
12-07 01:39:51.765 9756-9756/me.abhishekraj.showmyshow V/############******: onActivityCreated called and saveInstance is null
12-07 01:39:51.815 9756-9756/me.abhishekraj.showmyshow V/############******: onLoadFinished called with id 1111
12-07 01:39:51.817 9756-9756/me.abhishekraj.showmyshow V/############******: onLoadFinished called with id 9999
on rotate
12-07 01:40:18.441 9756-9756/me.abhishekraj.showmyshow V/############******: onSaveInstanceState called
12-07 01:40:18.472 9756-9756/me.abhishekraj.showmyshow V/############******: onCreate called
12-07 01:40:18.472 9756-9756/me.abhishekraj.showmyshow V/############******: onCreate savedInstance is not empty
12-07 01:40:18.478 9756-9756/me.abhishekraj.showmyshow V/############******: onCreateView savedInstance is Bundle[{upcomingMovies=[me.abhishekraj.showmyshow.Movie@c837a9f, me.abhishekraj.showmyshow.Movie@42b07b5, me.abhishekraj.showmyshow.Movie@da99e4a,.......and so on..that means it's not empty
onRotating back
12-07 01:40:48.244 9756-9756/me.abhishekraj.showmyshow V/############******: onSaveInstanceState called
12-07 01:40:48.281 9756-9756/me.abhishekraj.showmyshow V/############******: onCreate called
12-07 01:40:48.281 9756-9756/me.abhishekraj.showmyshow V/############******: onCreate savedInstance is not empty
12-07 01:40:48.286 9756-9756/me.abhishekraj.showmyshow V/############******: onCreateView savedInstance is Bundle[{upcomingMovies=[me.abhishekraj.showmyshow.Movie@c837a9f, me.abhishekraj.showmyshow.Movie@42b07b5, me.abhishekraj.showmyshow.Movie@da99e4a, me.abhishekraj.showmyshow.Movie@67a86d8, me.abhishekraj.showmyshow.Movie@a516a31, me.abhishekraj.showmyshow.Movie@e241e97,.....and so on...that means it's not empty`

那么问题出在哪里?为什么当我从另一个活动返回到此片段时我得到空白的 saveInstanceState 但不是 null saveInstanceState 以进行屏幕旋转,即使在这两种情况下都再次创建了活动(???)。 谢谢!

编辑:

如果需要我的更多代码来获取参考,那么可以在以下位置找到它:Gist

【问题讨论】:

    标签: android-fragments android-recyclerview asynctaskloader


    【解决方案1】:

    如果将主要活动的启动模式用作单顶,则可以很容易地处理该问题,这在我的用例中似乎完全可以接受。这是解决问题后我的 manifest.xml 的样子:

        <activity
            android:name=".MainActivity"
            android:launchMode="singleTop">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
    
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    

    但是,更好的方法是使用Back Stack,这样可以在销毁之前保存活动状态。当我使用 back-stack 解决它时,我会更新答案,但现在它可以完成工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多