Skip to content

Commit

Permalink
implement onRestoreInstanceState method
Browse files Browse the repository at this point in the history
  • Loading branch information
Yang Zhou committed Jul 29, 2016
1 parent a2c9b1d commit c1282c8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
22 changes: 19 additions & 3 deletions app/src/main/java/org/esec/mcg/bleinsight/ScanDeviceActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.Manifest;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.le.ScanResult;
import android.content.DialogInterface;
import android.content.Intent;
Expand All @@ -29,6 +30,8 @@

import org.esec.mcg.library.logger.LogUtils;

import java.util.ArrayList;

public class ScanDeviceActivity extends Activity implements ScanDeviceUiCallbacks {
private static final int PERMISSION_REQUEST_COARSE_LOCATION = 1;

Expand All @@ -43,15 +46,14 @@ public class ScanDeviceActivity extends Activity implements ScanDeviceUiCallback

// private boolean isConfigurationChanged = false;
private boolean isRestart = false;
private boolean isRestore = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
LogUtils.e("onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_device_scan);

isRestart = false;

mScanDeviceAdapter = new ScanDeviceAdapter(this);

mRecyclerView = (RecyclerView) findViewById(R.id.device_list_view);
Expand Down Expand Up @@ -149,8 +151,15 @@ protected void onResume() {
finish();
}

if (!isRestart) {
Log.e("isRestore", "" + isRestore);
Log.e("list", "" + mScanDeviceAdapter.getItemCount());
if (!isRestart && !isRestore) {
startScanningInit();
} else {
mScanDeviceAdapter.notifyDataSetChanged();
scanToggle.setText("SCAN");
findViewById(R.id.toolbar_progress_bar).setVisibility(View.INVISIBLE);
mScanning = false;
}
}

Expand All @@ -166,19 +175,26 @@ protected void onPause() {
@Override
protected void onDestroy() {
LogUtils.e("onDestroy");
isRestart = false;
super.onDestroy();
}

@Override
protected void onSaveInstanceState(Bundle outState) {
LogUtils.e("onSaveInstanceState");
super.onSaveInstanceState(outState);
outState.putBoolean("isRestore", true);

outState.putParcelableArrayList("devices",mScanDeviceAdapter.getmDevices());
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
LogUtils.e("onRestoreInstanceState");
super.onRestoreInstanceState(savedInstanceState);
isRestore = savedInstanceState.getBoolean("isRestore");
ArrayList<BluetoothDevice> devices = savedInstanceState.getParcelableArrayList("devices");
mScanDeviceAdapter.setmDevices(devices);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public class ScanDeviceAdapter extends RecyclerView.Adapter<ScanDeviceAdapter.Li

private RecyclerView mRecyclerView;
private ItemSlideHelper mOnItemTouchListener;

private ArrayList<BluetoothDevice> mDevices;
private ArrayList<ScanRecord> mRecords;
private ArrayList<Integer> mRssis;
Expand All @@ -44,6 +43,14 @@ public class ScanDeviceAdapter extends RecyclerView.Adapter<ScanDeviceAdapter.Li

private Handler rssiUpdateHandler = new Handler();

public ArrayList<BluetoothDevice> getmDevices() {
return mDevices;
}

public void setmDevices(ArrayList<BluetoothDevice> mDevices) {
this.mDevices = mDevices;
}

public ScanDeviceAdapter(Activity parent) {
super();
mDevices = new ArrayList<BluetoothDevice>();
Expand Down

0 comments on commit c1282c8

Please sign in to comment.