Fixed Rotation Error

Solved problem where rotating the screen and then selecting a movie would cause the app to crash due to maintaining a reference to an old DetailFragment.
This commit is contained in:
Aargonian
2016-02-16 23:06:27 -05:00
parent eb17f3a98a
commit 2e0e032830
4 changed files with 46 additions and 28 deletions

2
.idea/misc.xml generated
View File

@@ -37,7 +37,7 @@
<ConfirmationsSetting value="0" id="Add" />
<ConfirmationsSetting value="0" id="Remove" />
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" />
</component>
<component name="ProjectType">

View File

@@ -1,11 +1,14 @@
package com.example.android.popularmovies;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@@ -22,14 +25,16 @@ import butterknife.ButterKnife;
*/
public class DetailFragment extends Fragment
{
private static final String LOG_TAG = DetailFragment.class.getSimpleName();
public DetailFragment() {}
private Movie movie;
@Bind(R.id.rating) TextView rating;
@Bind(R.id.releaseDate) TextView release;
@Bind(R.id.overview) TextView overview;
@Bind(R.id.titleView) TextView title;
@Bind(R.id.posterImage) ImageView poster;
private Movie movie;
private boolean viewCreated;
private boolean displayTitle = false;
@@ -54,10 +59,18 @@ public class DetailFragment extends Fragment
@SuppressWarnings("all") private final int VOTE_COUNT = 6;
@SuppressWarnings("all") private final int GENRES = 7;
@Override
public void onAttach(Context context)
{
super.onAttach(context);
Log.d(LOG_TAG, "ATTACHED TO ACTIVITY");
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Log.d(LOG_TAG, "DETAIL FRAGMENT BEING CREATED");
Intent intent = this.getActivity().getIntent();
if(savedInstanceState != null)
{
@@ -90,6 +103,7 @@ public class DetailFragment extends Fragment
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d(LOG_TAG, "DETAIL FRAGMENT VIEW BEING CREATED");
View root = inflater.inflate(R.layout.fragment_detail, container, false);
ButterKnife.bind(this, root);
if(!displayTitle)
@@ -113,6 +127,27 @@ public class DetailFragment extends Fragment
return root;
}
@Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
Log.d(LOG_TAG, "ACTIVITY CREATED");
}
@Override
public void onStart()
{
super.onStart();
Log.d(LOG_TAG, "FRAGMENT STARTED");
}
@Override
public void onResume()
{
super.onResume();
Log.d(LOG_TAG, "FRAGMENT RESUMED");
}
@Override
public void onSaveInstanceState(Bundle bundle)
{
@@ -154,6 +189,7 @@ public class DetailFragment extends Fragment
return genres.split("_");
}
@SuppressLint("SetTextI18n")
private void setMovieInfo(Movie movie)
{
this.movie = movie;

View File

@@ -3,6 +3,7 @@ package com.example.android.popularmovies;
import android.content.Intent;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
@@ -16,7 +17,7 @@ public class MainActivity extends AppCompatActivity
implements DiscoverFragment.OnMovieSelectedListener
{
private static final String LOG_TAG = MainActivity.class.getSimpleName();
private DetailFragment details;
private static final String DETAIL_FRAGMENT_TAG = "DetailFragmentTag";
@Override
protected void onCreate(Bundle savedInstanceState) {
@@ -35,8 +36,7 @@ public class MainActivity extends AppCompatActivity
if(this.findViewById(R.id.detail_container) != null)
{
transaction = fm.beginTransaction();
details = new DetailFragment();
transaction.add(R.id.detail_container, details);
transaction.add(R.id.detail_container, new DetailFragment(), DETAIL_FRAGMENT_TAG);
transaction.commit();
}
}
@@ -61,26 +61,14 @@ public class MainActivity extends AppCompatActivity
@Override
public void onMovieSelected(Long movieID)
{
if(findViewById(R.id.detail_container) != null && details == null)
{
details = new DetailFragment();
FragmentManager fm = this.getSupportFragmentManager();
FragmentTransaction transaction =fm.beginTransaction();
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
transaction.add(R.id.detail_container, details);
transaction.addToBackStack("Replacement");
transaction.commit();
}
if(findViewById(R.id.detail_container) != null)
Fragment temp = getSupportFragmentManager().findFragmentByTag(DETAIL_FRAGMENT_TAG);
if(temp != null)
{
DetailFragment details = (DetailFragment)temp;
details.setMovie(movieID);
}
else
{
} else {
Intent detailIntent = new Intent(this, DetailActivity.class);
detailIntent.putExtra("MOVIE_ID", movieID);
detailIntent.putExtra("DISPLAY_TITLE", true);
this.startActivity(detailIntent);
}
}

View File

@@ -97,7 +97,6 @@ public class MovieProvider extends ContentProvider
if(rowsModified > 0)
//noinspection ConstantConditions
getContext().getContentResolver().notifyChange(uri, null);
db.close();
return rowsModified;
}
@@ -123,7 +122,6 @@ public class MovieProvider extends ContentProvider
//noinspection ConstantConditions
getContext().getContentResolver().notifyChange(uri, null);
db.close();
return returnUri;
}
@@ -146,7 +144,6 @@ public class MovieProvider extends ContentProvider
if(rowsModified > 0)
//noinspection ConstantConditions
getContext().getContentResolver().notifyChange(uri, null);
db.close();
return rowsModified;
}
@@ -185,12 +182,9 @@ public class MovieProvider extends ContentProvider
}
//noinspection ConstantConditions
getContext().getContentResolver().notifyChange(uri, null);
db.close();
return returnCount;
default:
int count = super.bulkInsert(uri, values);
db.close();
return count;
return super.bulkInsert(uri, values);
}
}