我有一个片段,其中包含一个图表视图,可以在方向更改时重置

时间:2016-10-19 08:22:49

标签: android android-fragments

public class homeFragment extends Fragment
{
    SharedPreferences sharedpreferences;
    private static final String selectedChildTracker = "selectedChild";
    static ArrayList<String> subjects;
    static csBar cShowProgress = csBar.getInstance();
    BarChart chart;
    Button loadChart;
    BarData data;
    View card = null;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater,@Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
    {

            card =  inflater.inflate(R.layout.home,null);
            chart = (BarChart) card.findViewById(R.id.chart);
            loadChart = (Button) card.findViewById(R.id.graph_loader);
            sharedpreferences = this.getActivity().getSharedPreferences(selectedChildTracker, Context.MODE_PRIVATE);

            final String activeChild = sharedpreferences.getString("selectedChild",null);
            final String week = "4";
            final String term = "First Term";
            final String session = "2015/2016";

            loadChart.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View view)
                {
                    Request(week,term,session,activeChild);
                }
            });

        return card;
    }
}

我遇到的问题是,当单击加载图表按钮时,图表会正确加载,但是当方向更改或用户访问另一个页面时,图表数据将被清除并且片段将被重置。我从来没有以这种方式使用片段,所以我对如何处理这个问题感到很困惑。

1 个答案:

答案 0 :(得分:1)

只需在应用程序标记

下的最明显文件中添加此内容即可
 android:screenOrientation="portrait"

这将处理屏幕旋转问题