如果活动开始/重新启动,则保持/恢复位置



我有一个主活动,它启动了一个新活动

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

活动包含带有列表项的回收器视图

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

  • 如果我通过按返回按钮返回我的主活动并重新单击启动 newListActivity(( 的按钮,滚动位置将丢失。

我的代码列表活动

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

保持我的位置的最佳方法是什么? 共享活动之间的偏好 ?其他解决方案?你有样品吗?

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

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

并在创建,恢复中阅读:

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

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

只需在您的清单文件中将以下行添加到您希望在屏幕旋转时保持位置的活动。

android:configChanges="keyboardHidden|screenSize|orientation"

您需要对代码进行以下更改。基本上,您在活动不再处于活动状态时保存项目,然后在创建适配器时而不是在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();

    }
}

(这应该是对@Dominika答案的评论,但我没有声誉(而不是在共享首选项中保存位置,尝试在单例对象中保存位置。这样,位置将在应用程序运行时保存,当您杀死应用程序时,位置将丢失。

相关内容

  • 没有找到相关文章

最新更新