活动开始/重启时保持/恢复位置

时间:2017-11-09 11:00:59

标签: android android-activity

我有一个主动,开始一个新的活动

private void newListActivity(){
    Context context = MainActivity.this;
    Class destinationActivity = ListActivity.class;
    Intent startChildActivityIntent = new Intent(this.getApplicationContext(), destinationActivity);
    startActivity(startChildActivityIntent);
}

ListActivity包含带有列表项

的recyclerview
  • 如果我在列表中滚动并旋转屏幕,则会保留位置。

  • 如果我通过按返回按钮返回我的mainActivity并重新点击我的按钮以启动newListActivity(),则滚动位置将丢失。

我的代码ListActivity

import android.content.SharedPreferences;
import android.os.Environment;
import android.os.Parcelable;
import android.os.PersistableBundle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

import java.util.ArrayList;

public class ListActivity extends AppCompatActivity {


    ArrayList<Trace> _traceDataset = new ArrayList<Trace>();
    protected RecyclerView mRecyclerView;
    protected RecyclerView.LayoutManager mLayoutManager;
    TraceAdapter mAdapter ;
    String TAG = this.getClass().getSimpleName().toUpperCase();



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_trace_list);
        mRecyclerView = (RecyclerView) findViewById(R.id.rvTrace);
        initList();
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setHasFixedSize(true);
        mAdapter = new TraceAdapter(_traceDataset);
        // Set CustomAdapter as the adapter for RecyclerView.
        mRecyclerView.setAdapter(mAdapter);

        if (savedInstanceState != null) {
            // Then the application is being reloaded
            Log.v(TAG, "savedInstanceState non null-> activity reloaded".toUpperCase());
        }else{
            Log.v(TAG, "savedInstanceState  null-> first activity load".toUpperCase());
        }
    }

    public void initList(){
        for (int i = 0; i < 200; i++) {
            Obj obj = new Obj();
            obj.setName(" GPS " + i);
            obj.setIndex("Element #" + i);
            _traceDataset.add(trace);
        }
        Log.v(TAG, _traceDataset.size());
    }

   /* @Override
    public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
        super.onSaveInstanceState(outState);
        Log.v("onSaveInstanceState","onSaveInstanceState");
    }*/


    private int someVarA;


       @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        //outState.putInt("someVarA", someVarA);
        Log.v("onSaveInstanceState","onSaveInstanceState");

    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        //someVarA = savedInstanceState.getInt("someVarA");
        Log.v("onRestoreInstanceState","onRestoreInstanceState");


    }


    @Override
    public void onPause() {
        // Save ListView state @ onPause
        Log.d(TAG, "ON PAUSE");
        super.onPause();



    }
}

显然,我无法使用onRestoreInstanceState和onSaveInstanceState

保持职位的最佳方法是什么?活动之间的共享参考? 其他方案? 你有样品吗?

4 个答案:

答案 0 :(得分:0)

我建议使用在onDestroy和onPause中保存位置:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = prefs.edit();
editor.put("savedPosition", position);
editor.commit();

并阅读onCreate,onResume:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
int savedPosition = prefs.getInt("savedPosition", 0);

其中第二个参数是默认值(此处为0)

答案 1 :(得分:0)

只需在清单文件中添加以下行,即可在屏幕旋转时保留位置的活动。

android:configChanges="keyboardHidden|screenSize|orientation"

答案 2 :(得分:0)

您需要对代码进行以下更改。基本上,当活动不再处于活动状态时保存项目,然后在创建适配器而不是onRestoreInstanceState事件时恢复它们。

import android.content.SharedPreferences;
import android.os.Environment;
import android.os.Parcelable;
import android.os.PersistableBundle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

import java.util.ArrayList;

public class ListActivity extends AppCompatActivity {


    ArrayList<Trace> _traceDataset;
    protected RecyclerView mRecyclerView;
    protected RecyclerView.LayoutManager mLayoutManager;
    TraceAdapter mAdapter ;
    String TAG = this.getClass().getSimpleName().toUpperCase();



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_trace_list);
        mRecyclerView = (RecyclerView) findViewById(R.id.rvTrace);
        initList();
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setHasFixedSize(true);


        if (savedInstanceState != null) {

            // get saved items
            _traceDataset   = (ArrayList<Trace>)savedInstanceState.getSerializable("items");

        }else{

            //create new list
            _traceDataset = new ArrayList<Trace>();
            //add some items to your list for testing
            initList();
        }
        // Set CustomAdapter as the adapter for RecyclerView.
        mAdapter =TraceAdapter(_traceDataset);
        mRecyclerView.setAdapter(mAdapter);
    }

    public void initList(){
        for (int i = 0; i < 200; i++) {
            Obj obj = new Obj();
            obj.setName(" GPS " + i);
            obj.setIndex("Element #" + i);
            _traceDataset.add(trace);
        }
        Log.v(TAG, _traceDataset.size());
    }

   /* @Override
    public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
        super.onSaveInstanceState(outState);
        outState.putSerializable("items", _traceDataset);
    }*/


    private int someVarA;


       @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        //outState.putInt("someVarA", someVarA);
        outState.putSerializable("items", items);

    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        //someVarA = savedInstanceState.getInt("someVarA");
        Log.v("onRestoreInstanceState","onRestoreInstanceState");


    }


    @Override
    public void onPause() {
        // Save ListView state @ onPause
        Log.d(TAG, "ON PAUSE");
        super.onPause();



    }
}

答案 3 :(得分:0)

(这应该是对@Dominika的答案的评论,但我没有声誉)而不是在SharedPreferences中保存位置,尝试在Singleton对象中保存位置。这样,在应用程序运行时将保存位置,当您终止应用程序时,位置将丢失。