Android 配置更改时列表片段未更新

Android list fragment not updated when configuration changes

我正在制作一个简单的应用程序,它向主页中的列表片段添加一个位置,并且当我向列表中添加更多地址时,每当配置更改时,这些地址都会被保留,这是预期的。

然而,正如您将在下面看到的,由于某种原因出现了空列表文本,似乎当我以后添加另一个新地址时,它会添加到列表的第一位。

下面是我的主要内容 activity :

public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {

private GoogleApiClient mGoogleApiClient;
private Location mLastLocation, mCurrentLocation;
private double currentLatitude, currentLongitude;
private String lastUpdateTime, addressMessage = null;
private AddressResultReceiver resultReceiver;
private MainList listFragment = new MainList();
//private boolean mRequestStatus = true;
private MaterialDialog dialog;
public boolean mAddressRequested = false;
private FragmentManager mFragmentManager;
private final String FRAGMENT_TAG = "main_list_tag";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApi(LocationServices.API)
            .addOnConnectionFailedListener(this)
            .addConnectionCallbacks(this)
            .build();

    mFragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
    fragmentTransaction.add(R.id.list_frame, listFragment);
    fragmentTransaction.commit();

    dialog = new MaterialDialog(this)
            .setTitle("Select an address")
            .setPositiveButton("SELECT", new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                    listFragment.list.add(addressMessage);
                    listFragment.mAdapter.notifyDataSetChanged();
                }
            })
            .setNegativeButton("CANCEL", new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });

    Toolbar toolbar = (Toolbar) findViewById(R.id.tool_bar);
    setSupportActionBar(toolbar);

    FloatingActionButton floatingActionButton = (FloatingActionButton) findViewById(R.id.fab_button);

    //LayoutInflater inflater = LayoutInflater.from(getApplicationContext());

    floatingActionButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (checkAirPlaneMode(getApplicationContext())) {
                Toast.makeText(getApplicationContext(), "Air Plane Mode is ON, Please turn off to get location", Toast.LENGTH_LONG).show();
            }
            else if (!checkNetwork()) {
                Toast.makeText(getApplicationContext(),"Unable to reach network, please check network settings",Toast.LENGTH_LONG).show();
            }
            else if (!checkLocationSettings(getApplicationContext())) {
                showLocationSettings();
            }
            else {
                startAddressLookUp();
                mAddressRequested = true;
                dialog.setMessage(addressMessage);
                dialog.show();
            }
        }
    });

}


@Override
public void onStart() {
    super.onStart();
    if (mGoogleApiClient == null) {
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addApi(LocationServices.API)
                .addOnConnectionFailedListener(this)
                .addConnectionCallbacks(this)
                .build();
    }
    mGoogleApiClient.connect();
}

@Override
protected void onPause() {
    super.onPause();
    LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
}

@Override
public void onResume() {
    super.onResume();
    if(mGoogleApiClient.isConnected()) {
        LocationRequest locationRequest = createLocationRequest();
        startLocationUpdates(locationRequest);
    }
}

和列表片段class:

public class MainList extends ListFragment implements AdapterView.OnItemClickListener {

private int stateInt;
private final String FRAGMENT_KEY = "saved_fragment";
ArrayList<String> list = new ArrayList<>();
ArrayAdapter<String> mAdapter;

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

    return inflater.inflate(R.layout.list_fragment, container, false);

    /*ListView view = (ListView) v.findViewById(android.R.id.list);
    view.setEmptyView(v.findViewById(android.R.id.empty));

    return view;*/
}

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

    setRetainInstance(true);

    getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);

    mAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, list);

    setListAdapter(mAdapter);
    //mAdapter.notifyDataSetChanged();
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    Toast.makeText(parent.getContext(), "Item Pressed!", Toast.LENGTH_LONG).show();
}

}

如何避免这种情况?我是否缺少保存应用程序状态或其他内容?任何帮助,将不胜感激!提前致谢!

谢谢, 保罗

每次方向改变都会调用onCreate方法。并在方法中再次添加片段。这就是为什么你会出现这种行为。

检查此以了解执行此操作的正确方法。 Handling Configuration Changes with Fragments

FragmentManager fm = getFragmentManager();
mTaskFragment = (TaskFragment) fm.findFragmentByTag(TAG_TASK_FRAGMENT);

// If the Fragment is non-null, then it is currently being
// retained across a configuration change.
if (mTaskFragment == null) {
   mTaskFragment = new TaskFragment();
   fm.beginTransaction().add(mTaskFragment, TAG_TASK_FRAGMENT).commit();
}