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.LocationManager;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.provider.Settings;
import android.support.v4.app.Fragment;
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 double MAX_FRAGMENT_ID = WIKI_FRAGMENT_ID;
private static final double MIN_FRAGMENT_ID = HOME_FRAGMENT_ID;
private static final String FRAG_ID = "FRAG_ID";
public static DisplayImageOptions options;
private GestureDetectorCompat gestureDetector;
private float SWIPE_THRESHOLD = 50;
@ -67,7 +69,11 @@ public class MainActivity extends ActionBarActivity implements GestureDetector.O
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.v(CLASS_TAG, "onCreate");
setContentView(R.layout.activity_main);
currentFragmentId = 0;
// Toolbar toolbar = (Toolbar) findViewById(R.id.navbar);
// setSupportActionBar(toolbar);
@ -84,10 +90,10 @@ public class MainActivity extends ActionBarActivity implements GestureDetector.O
ImageLoader.getInstance().init(config);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragments, new HomeFragment())
.commit();
// getSupportFragmentManager()
// .beginTransaction()
// .replace(R.id.fragments, new HomeFragment())
// .commit();
// lLinearLayout locations = (LinearLayout) findViewById(R.id.locations);
// locations.setOnTouchListener(new OnSwipeTouchListener);
@ -96,6 +102,13 @@ public class MainActivity extends ActionBarActivity implements GestureDetector.O
initLocalication();
Fabric fabric = new Fabric.Builder(this).debuggable(true).kits(new Crashlytics()).build();
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
@ -303,7 +316,7 @@ public class MainActivity extends ActionBarActivity implements GestureDetector.O
@Override
protected void onResume() {
super.onResume();
Log.v(CLASS_TAG, "onResume");
if (locationProvider != null) {
locationService.requestLocationUpdates(locationProvider,
AppConstants.MIN_TIME,
@ -317,11 +330,18 @@ public class MainActivity extends ActionBarActivity implements GestureDetector.O
@Override
protected void onPause() {
super.onPause();
Log.v(CLASS_TAG, "onPause");
if (locationServicesActivated) {
locationService.removeUpdates(locationCallback);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.v(CLASS_TAG, "onDestroy");
}
@Override
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);
}
}
@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.LocationManager;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.androidquery.AQuery;
import pl.tpolgrabia.urbanexplorer.MainActivity;
import pl.tpolgrabia.urbanexplorer.R;
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 LocationManager locationService;
private AQuery aq;
private boolean initialized = false;
private View inflatedView;
private Long pageId = 1L;
private Semaphore loading = new Semaphore(1, true);
private List<PanoramioImageInfo> photos = new ArrayList<>();
private Long pageId;
private Semaphore loading;
private List<PanoramioImageInfo> photos;
private String locationProvider;
private boolean noMorePhotos = false;
private boolean noMorePhotos;
public HomeFragment() {
// Required empty public constructor
@ -50,7 +49,21 @@ public class HomeFragment extends Fragment {
@Override
public void onCreate(Bundle 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.getLocationCallback()
.addCallback(new StandardLocationListenerCallback() {
@ -66,7 +79,6 @@ public class HomeFragment extends Fragment {
}
}
});
}
private Double safeParseDouble(CharSequence text) {
@ -273,6 +285,7 @@ public class HomeFragment extends Fragment {
@Override
public void onResume() {
super.onResume();
Log.v(CLASS_TAG, "onResume");
locationProvider = LocationUtils.getDefaultLocation(getActivity());
updateLocationInfo();
}
@ -292,4 +305,24 @@ public class HomeFragment extends Fragment {
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
public void onClick(View v) {
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 radius_limit = ((EditText) inflatedView.findViewById(R.id.wiki_search_radius)).getText();