Handling gratefully screen rotation and configuration changes.

master
Tomasz Półgrabia 2016-09-12 23:11:20 +02:00
parent 60f7f2aaa8
commit 40cb00b28a
3 changed files with 83 additions and 13 deletions

View File

@ -5,6 +5,7 @@ import android.content.Intent;
import android.location.Location; import android.location.Location;
import android.location.LocationManager; import android.location.LocationManager;
import android.os.Bundle; import android.os.Bundle;
import android.os.PersistableBundle;
import android.provider.Settings; import android.provider.Settings;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentManager;
@ -40,6 +41,7 @@ public class MainActivity extends ActionBarActivity implements GestureDetector.O
private static final int WIKI_FRAGMENT_ID = 1; private static final int WIKI_FRAGMENT_ID = 1;
private static final double MAX_FRAGMENT_ID = WIKI_FRAGMENT_ID; private static final double MAX_FRAGMENT_ID = WIKI_FRAGMENT_ID;
private static final double MIN_FRAGMENT_ID = HOME_FRAGMENT_ID; private static final double MIN_FRAGMENT_ID = HOME_FRAGMENT_ID;
private static final String FRAG_ID = "FRAG_ID";
public static DisplayImageOptions options; public static DisplayImageOptions options;
private GestureDetectorCompat gestureDetector; private GestureDetectorCompat gestureDetector;
private float SWIPE_THRESHOLD = 50; private float SWIPE_THRESHOLD = 50;
@ -67,7 +69,11 @@ public class MainActivity extends ActionBarActivity implements GestureDetector.O
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
Log.v(CLASS_TAG, "onCreate");
setContentView(R.layout.activity_main); setContentView(R.layout.activity_main);
currentFragmentId = 0;
// Toolbar toolbar = (Toolbar) findViewById(R.id.navbar); // Toolbar toolbar = (Toolbar) findViewById(R.id.navbar);
// setSupportActionBar(toolbar); // setSupportActionBar(toolbar);
@ -84,10 +90,10 @@ public class MainActivity extends ActionBarActivity implements GestureDetector.O
ImageLoader.getInstance().init(config); ImageLoader.getInstance().init(config);
getSupportFragmentManager() // getSupportFragmentManager()
.beginTransaction() // .beginTransaction()
.replace(R.id.fragments, new HomeFragment()) // .replace(R.id.fragments, new HomeFragment())
.commit(); // .commit();
// lLinearLayout locations = (LinearLayout) findViewById(R.id.locations); // lLinearLayout locations = (LinearLayout) findViewById(R.id.locations);
// locations.setOnTouchListener(new OnSwipeTouchListener); // locations.setOnTouchListener(new OnSwipeTouchListener);
@ -96,6 +102,13 @@ public class MainActivity extends ActionBarActivity implements GestureDetector.O
initLocalication(); initLocalication();
Fabric fabric = new Fabric.Builder(this).debuggable(true).kits(new Crashlytics()).build(); Fabric fabric = new Fabric.Builder(this).debuggable(true).kits(new Crashlytics()).build();
Fabric.with(fabric); Fabric.with(fabric);
Integer fragId = savedInstanceState != null ? savedInstanceState.getInt(FRAG_ID) : null;
Log.v(CLASS_TAG, "Restored orig frag id: " + fragId);
currentFragmentId = fragId == null ? 0 : fragId;
Log.v(CLASS_TAG, "Set final frag id: " + fragId);
switchFragment();
} }
@Override @Override
@ -303,7 +316,7 @@ public class MainActivity extends ActionBarActivity implements GestureDetector.O
@Override @Override
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
Log.v(CLASS_TAG, "onResume");
if (locationProvider != null) { if (locationProvider != null) {
locationService.requestLocationUpdates(locationProvider, locationService.requestLocationUpdates(locationProvider,
AppConstants.MIN_TIME, AppConstants.MIN_TIME,
@ -317,11 +330,18 @@ public class MainActivity extends ActionBarActivity implements GestureDetector.O
@Override @Override
protected void onPause() { protected void onPause() {
super.onPause(); super.onPause();
Log.v(CLASS_TAG, "onPause");
if (locationServicesActivated) { if (locationServicesActivated) {
locationService.removeUpdates(locationCallback); locationService.removeUpdates(locationCallback);
} }
} }
@Override
protected void onDestroy() {
super.onDestroy();
Log.v(CLASS_TAG, "onDestroy");
}
@Override @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) { public void onActivityResult(int requestCode, int resultCode, Intent data) {
@ -339,4 +359,14 @@ public class MainActivity extends ActionBarActivity implements GestureDetector.O
super.onActivityResult(requestCode, resultCode, data); super.onActivityResult(requestCode, resultCode, data);
} }
} }
@Override
protected void onSaveInstanceState(Bundle outState) {
Log.v(CLASS_TAG, "1 Saving current fragment id: " + currentFragmentId);
super.onSaveInstanceState(outState);
outState.putSerializable(FRAG_ID, currentFragmentId);
Log.v(CLASS_TAG, "2 Saving current fragment id: " + currentFragmentId);
}
} }

View File

@ -4,13 +4,13 @@ import android.content.Context;
import android.location.Location; import android.location.Location;
import android.location.LocationManager; import android.location.LocationManager;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.util.Log; import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.*; import android.widget.*;
import com.androidquery.AQuery;
import pl.tpolgrabia.urbanexplorer.MainActivity; import pl.tpolgrabia.urbanexplorer.MainActivity;
import pl.tpolgrabia.urbanexplorer.R; import pl.tpolgrabia.urbanexplorer.R;
import pl.tpolgrabia.urbanexplorer.callbacks.PanoramioResponseCallback; import pl.tpolgrabia.urbanexplorer.callbacks.PanoramioResponseCallback;
@ -33,15 +33,14 @@ public class HomeFragment extends Fragment {
private static final int PANORAMIA_BULK_DATA_SIZE = 10; private static final int PANORAMIA_BULK_DATA_SIZE = 10;
private LocationManager locationService; private LocationManager locationService;
private AQuery aq;
private boolean initialized = false; private boolean initialized = false;
private View inflatedView; private View inflatedView;
private Long pageId = 1L; private Long pageId;
private Semaphore loading = new Semaphore(1, true); private Semaphore loading;
private List<PanoramioImageInfo> photos = new ArrayList<>(); private List<PanoramioImageInfo> photos;
private String locationProvider; private String locationProvider;
private boolean noMorePhotos = false; private boolean noMorePhotos;
public HomeFragment() { public HomeFragment() {
// Required empty public constructor // Required empty public constructor
@ -50,7 +49,21 @@ public class HomeFragment extends Fragment {
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
aq = new AQuery(getActivity()); Log.v(CLASS_TAG, "onCreate");
pageId = 1L;
loading = new Semaphore(1, true);
photos = new ArrayList<>();
noMorePhotos = false;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
initLocationCallback();
}
private void initLocationCallback() {
MainActivity mainActivity = ((MainActivity) getActivity()); MainActivity mainActivity = ((MainActivity) getActivity());
mainActivity.getLocationCallback() mainActivity.getLocationCallback()
.addCallback(new StandardLocationListenerCallback() { .addCallback(new StandardLocationListenerCallback() {
@ -66,7 +79,6 @@ public class HomeFragment extends Fragment {
} }
} }
}); });
} }
private Double safeParseDouble(CharSequence text) { private Double safeParseDouble(CharSequence text) {
@ -273,6 +285,7 @@ public class HomeFragment extends Fragment {
@Override @Override
public void onResume() { public void onResume() {
super.onResume(); super.onResume();
Log.v(CLASS_TAG, "onResume");
locationProvider = LocationUtils.getDefaultLocation(getActivity()); locationProvider = LocationUtils.getDefaultLocation(getActivity());
updateLocationInfo(); updateLocationInfo();
} }
@ -292,4 +305,24 @@ public class HomeFragment extends Fragment {
locationInfo.setText("Location: " + currLocation.getLatitude() + "," + currLocation.getLongitude()); locationInfo.setText("Location: " + currLocation.getLatitude() + "," + currLocation.getLongitude());
} }
} }
@Override
public void onPause() {
super.onPause();
Log.v(CLASS_TAG, "onPause");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.v(CLASS_TAG, "onDestroy");
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Log.v(CLASS_TAG, "Saving state");
}
} }

View File

@ -72,6 +72,13 @@ public class WikiLocationsFragment extends Fragment {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
final Location location = locationService.getLastKnownLocation(LocationUtils.getDefaultLocation(getActivity())); final Location location = locationService.getLastKnownLocation(LocationUtils.getDefaultLocation(getActivity()));
if (location == null) {
Log.i(CLASS_TAG, "Sorry, location is still not available");
Toast.makeText(getActivity(), "Sorry, location is still not available", Toast.LENGTH_SHORT).show();
return;
}
Editable search_limit = ((EditText) inflatedView.findViewById(R.id.wiki_search_limit)).getText(); Editable search_limit = ((EditText) inflatedView.findViewById(R.id.wiki_search_limit)).getText();
Editable radius_limit = ((EditText) inflatedView.findViewById(R.id.wiki_search_radius)).getText(); Editable radius_limit = ((EditText) inflatedView.findViewById(R.id.wiki_search_radius)).getText();