防止在设备旋转时重新创建Android片段

时间:2015-07-22 19:29:47

标签: java android android-fragments android-activity

问题:

当我旋转设备时,我的应用程序中的片段中包含的信息将被重新加载。由于这个应用程序从互联网上提取信息,这意味着在重新收集数据的过程中缺少数据。

我的尝试:

我已阅读了许多文章和S / O问题,这些问题提供了一些一般性的指导并尝试了他们的解决方案,从清单中的android:configChanges="orientation|screenSize"到我setRetainInstance(true);MainActivity中的FragmentActivity android:configChanges。这些都没有阻止我的片段重新加载。我在应用程序中的另一个活动中使用了MainActivity.class(一个详细活动,当用户选择新闻报道时会激活)并且在那里正常工作,阻止活动重新加载。

我的应用程序的结构:

这是一个简单的应用程序,它从“黑客新闻”API中提取数据并将其显示给用户。有一个NavigationDrawer供用户选择不同的新闻类型。 NavigationDrawer中的每个项目都是在HomeFragment.class中加载的不同片段。这里讨论的片段是package com.material.tdapps.hackernews.activity; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.Menu; import android.view.MenuItem; import android.view.View; import com.material.tdapps.hackernews.R; public class MainActivity extends AppCompatActivity implements FragmentDrawer.FragmentDrawerListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar appToolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(appToolbar); //noinspection ConstantConditions getSupportActionBar().setDisplayShowHomeEnabled(true); FragmentDrawer drawerFragment = (FragmentDrawer) getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer); drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), appToolbar); drawerFragment.setDrawerListener(this); displayView(0); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); return id == R.id.action_settings || super.onOptionsItemSelected(item); } @Override public void onDrawerItemSelected(View view, int position) { displayView(position); } private void displayView(int position){ Fragment fragment = null; String title = getString(R.string.app_name); switch (position) { case 0: fragment = new HomeFragment(); title = getString(R.string.title_news); break; case 1: fragment = new ShowFragment(); title = getString(R.string.title_show); break; case 2: fragment = new AskFragment(); title = getString(R.string.title_ask); break; case 3: fragment = new JobsFragment(); title = getString(R.string.title_job); break; default: break; } if (fragment != null){ FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.replace(R.id.container_body, fragment); fragmentTransaction.commit(); getSupportActionBar().setTitle(title); } } } ,它载有主要的新闻报道。

为什么我在这里问:

我似乎无法找到任何与我目前的应用程序结构一致的内容,所以我希望能帮助我找到解决这个看似微不足道(但令人困惑)问题的解决方案。

代码:

MainActivity.java

package com.material.tdapps.hackernews.activity;

import android.app.Activity;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.material.tdapps.hackernews.JSON.JSONNewsParser;
import com.material.tdapps.hackernews.R;
import com.material.tdapps.hackernews.model.StoryFeed;
import com.material.tdapps.hackernews.model.StoryItem;

import java.net.MalformedURLException;


public class HomeFragment extends Fragment {

    StoryFeed storyFeed;
    Context context;
    ListView listView;
    NewsListAdapter newsListAdapter;

    public HomeFragment() {

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_home, container, false);
        setRetainInstance(true);
        context = this.getActivity();
        storyFeed = new StoryFeed();
        newsListAdapter = new NewsListAdapter(this);
        listView = (ListView) rootView.findViewById(R.id.newsListView);
        if (savedInstanceState != null && (savedInstanceState.getSerializable("previousFeed") != null)) {
            Log.e("NOTIFY >> ", "GOING TO BUNDLE");
            storyFeed = (StoryFeed)savedInstanceState.getSerializable("previousFeed");
            listView.setAdapter(newsListAdapter);
            newsListAdapter.notifyDataSetChanged();
        } else {
            Log.e("NOTIFY >> ", "GOING TO ASYNCTASK");
            new AsyncLoadNewsFeed().execute();
            listView.setAdapter(newsListAdapter);
        }

        setRetainInstance(true);

        return rootView;

    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    private class AsyncLoadNewsFeed extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {
            JSONNewsParser newsParser = new JSONNewsParser();
            storyFeed = newsParser.parseJSON("https://hacker-news.firebaseio.com/v0/topstories.json",0,30);
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            if (storyFeed == null || storyFeed.getStoryCount() == 0){
                StoryItem nullStoryItem = new StoryItem();
                nullStoryItem.setTitle("ERROR: Null error!");
                nullStoryItem.setNumberComments(0);
                nullStoryItem.setScore(0);
                nullStoryItem.setBodyText("NULL");
                nullStoryItem.setTime(0);
                nullStoryItem.setAuthor("NULL");
                nullStoryItem.setUrl("http://www.google.com");
                storyFeed.addStory(nullStoryItem);
            }
            newsListAdapter.notifyDataSetChanged();
        }
    }

    class NewsListAdapter extends BaseAdapter {

        private LayoutInflater layoutInflater;

        public NewsListAdapter(HomeFragment homeFragment){
            layoutInflater = (LayoutInflater) homeFragment.getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        @Override
        public int getCount() {
            return storyFeed.getStoryCount();
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        class listViewHolder {

            TextView titleTxtV;
            TextView timeTxtV;
            TextView scoreTxtV;
            TextView authorTxtV;
            TextView domTxtV;
            Button commentB;
            RelativeLayout detailsLayout;

            listViewHolder(View v) {
                titleTxtV = (TextView) v.findViewById(R.id.titleText);
                timeTxtV = (TextView) v.findViewById(R.id.timeText);
                scoreTxtV = (TextView) v.findViewById(R.id.pointsText);
                authorTxtV = (TextView) v.findViewById(R.id.authorText);
                domTxtV = (TextView) v.findViewById(R.id.domainText);
                commentB = (Button) v.findViewById(R.id.commentButton);
                detailsLayout = (RelativeLayout) v.findViewById(R.id.newsDetailRelLayout);
            }

        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {

            View listItem = convertView;
            listViewHolder holder;

            if (listItem == null) {
                listItem = layoutInflater.inflate(R.layout.news_item_layout, parent, false );
                holder = new listViewHolder(listItem);
                listItem.setTag(holder);
            } else {
                holder = (listViewHolder) listItem.getTag();
            }



            holder.titleTxtV.setText(storyFeed.getStory(position).getTitle());
            holder.timeTxtV.setText("Posted " + storyFeed.getStory(position).getTime());
            holder.authorTxtV.setText(" By " + storyFeed.getStory(position).getAuthor());
            holder.scoreTxtV.setText(storyFeed.getStory(position).getScore() + " Points ");
            holder.commentB.setText(Integer.toString(storyFeed.getStory(position).getNumberComments()));
            try {
                holder.domTxtV.setText("(" + storyFeed.getStory(position).getURLDomain() + ")");
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }

            holder.commentB.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent showComments = new Intent(getActivity(), CommentActivity.class);
                    getActivity().startActivity(showComments);
                }
            });

            holder.detailsLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent showDetails = new Intent(getActivity(), DetailWebActivity.class);
                    Bundle detailsBundle = new Bundle();
                    //detailsBundle.putSerializable("newsFeed", storyFeed);
                    showDetails.putExtra("news",storyFeed);
                    showDetails.putExtras(detailsBundle);
                    showDetails.putExtra("position", position);
                    getActivity().startActivity(showDetails);
                }
            });



            return listItem;

        }
    }



}

HomeFragment.java

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.material.tdapps.hackernews" >

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/HackerNewsTheme" >
        <activity
            android:name=".activity.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".activity.CommentActivity"
            android:label="@string/title_activity_comment"
            android:parentActivityName=".activity.MainActivity"
            android:configChanges="orientation|screenSize">

            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.material.tdapps.hackernews.activity.MainActivity" />
        </activity>
        <activity
            android:name=".activity.DetailWebActivity"
            android:label="@string/title_activity_detail_web"
            android:parentActivityName=".activity.MainActivity"
            android:configChanges="orientation|screenSize">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.material.tdapps.hackernews.activity.MainActivity" />
        </activity>
    </application>

</manifest>

的AndroidManifest.xml

DataFormatString="{0:c}"

2 个答案:

答案 0 :(得分:7)

调用setRetainInstance(true)会阻止重新创建Fragment。但是,现在Activity 始终重新创建DrawerFragment并强制重新创建HomeFragment,从而导致您看到的行为。在Activity.onCreate()检查savedInstanceStatenull。如果是,请创建Fragment。如果没有,则不要因为系统会自动恢复它们。

答案 1 :(得分:0)

您始终在Fragment方法中创建新的displayView。相反,您应该使用FragmentManager按ID或按标签查找您的片段,然后将其放入容器视图中。

请参阅http://developer.android.com/reference/android/app/FragmentManager.html#findFragmentByTag(java.lang.String)

修改

实际上@Larry Schiefer是对的,你甚至不需要找到一个片段,只需检查已保存的实例状态是否为空。