-1

When selecting a menu option item in landscape mode it seems not to work, it just has a progress loader but when in portrait mode I can select the menu item option and does expected task, it loads new loader and shows items in a gridview.

public class MainActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<List<MovieImageData>>{

private final String LOG_TAG = MainActivity.class.getSimpleName();
//API KEY
private static final String API_KEY = "";

private static final String BASE_SORT_URL = "http://api.themoviedb.org/3/movie";
//Popular movie query
private static final String POPULAR_MOVIES_TAG = "/popular?api_key=";
//Top rated movie query
private static final String TOP_RATED_MOVIES_TAG = "/top_rated?api_key=";

private static final String POPULAR_MOVIES_URL = BASE_SORT_URL + POPULAR_MOVIES_TAG + API_KEY;
private static final String TOP_RATED_MOVIES_URL = BASE_SORT_URL + TOP_RATED_MOVIES_TAG + API_KEY;

private static String currentURL = POPULAR_MOVIES_URL;
static boolean hasLoaderOne = false;
MoviePosterAdapter adapter;
GridView moviePosterGridView;

View progress;

private static ArrayList<MovieImageData> movieData;

//Movie base
private static final String MOVIEDB_BASE_URL = "http://api.themoviedb.org/3/movie";
//Image base URL
private static final String MOVIEDB_IMAGE_BASE = "http://image.tmdb.org/t/p/w185/";

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

    progress = findViewById(R.id.loading_spinner);

    if(savedInstanceState == null || !savedInstanceState.containsKey("movie")) {
        Log.i(LOG_TAG, "Ent======================================================================");
        progress.setVisibility(View.GONE);
        getLoaderManager().initLoader(0, null, this).forceLoad();
    }else {

        movieData = savedInstanceState.getParcelableArrayList("movie");
        updateUi(movieData);
    }
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.settings, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {

        case R.id.popular:
            currentURL = POPULAR_MOVIES_URL;
            getLoaderManager().getLoader(0).forceLoad();
            progress.setVisibility(View.VISIBLE);
            hasLoaderOne = false;
            return true;


    case R.id.top_rated:

            currentURL = TOP_RATED_MOVIES_URL;
            getLoaderManager().initLoader(1, null, this).forceLoad();
            hasLoaderOne = true;
            progress.setVisibility(View.VISIBLE);
            return true;
    }
    return super.onOptionsItemSelected(item);
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    outState.putParcelableArrayList("movie", movieData);
    super.onSaveInstanceState(outState);
}

@Override
public Loader<List<MovieImageData>> onCreateLoader(int id, Bundle args) {
    Log.i(LOG_TAG, "LOADER============================");
    progress.setVisibility(View.VISIBLE);
    return new MovieImageLoader(MainActivity.this, currentURL);
}

@Override
public void onLoadFinished(Loader<List<MovieImageData>> loader, List<MovieImageData> data) {
    progress.setVisibility(View.GONE);
    movieData = new ArrayList<MovieImageData>();
    movieData.addAll(data);
    updateUi(movieData);
}

@Override
public void onLoaderReset(Loader<List<MovieImageData>> loader) {
    progress.setVisibility(View.GONE);
    moviePosterGridView.setAdapter(null);
}

public void updateUi(final List<MovieImageData> moviePosterData){

    // Create a new {@link ArrayAdapter} of earthquakes
    adapter = new MoviePosterAdapter(this, moviePosterData);

    TextView noItemFound = (TextView) findViewById(R.id.no_list);
    // Find a reference to the {@link ListView} in the layout
    moviePosterGridView = (GridView) findViewById(R.id.gridview);
    moviePosterGridView.setEmptyView(noItemFound);

    if(!isOnline()){
        noItemFound.setVisibility(View.VISIBLE);
    }else{
        //noItemFound.setText("No earthquakes found!");
    }


    // Set the adapter on the {@link ListView}
    // so the list can be populated in the user interface
    moviePosterGridView.setAdapter(adapter);
}

public boolean isOnline() {
    ConnectivityManager cm =
            (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    return netInfo != null && netInfo.isConnectedOrConnecting();
}

}

IMAGES OF WHAT HAPPENS IN LANSCAPE MODDE enter image description here

enter image description here

The progressing loader continues and doesn't end until you rotate it back to portrait mode.

user2070739
  • 367
  • 1
  • 3
  • 15
  • and where are you hiding it(progress)? also it's completely misuse of loader ... you shouldn't call `forceLoad` (rather `initLoader`/`restartLoader`) ... you should not store `movieData` in `outState` (Loader should survive the Activity destruction on rotation and calling `initLoader` again should call `onLoadFinished` without creating new instance) – Selvin Sep 29 '17 at 07:47
  • The first time calling the second Loader I initialized it in the onOptionsItemSelected(MenuItem item) . What would you say the best way of achieving this? – user2070739 Sep 29 '17 at 07:51
  • I think activity recreate required value not get when you change the orientation of device. Try to android:configChanges="orientation|screenSize" put this line in manifest file inside your MainActivity declaration. – Pravin Fofariya Sep 29 '17 at 07:51
  • I think so to but how to resolve and why isn't it get the orientation – user2070739 Sep 29 '17 at 07:52
  • *Try to android:configChanges="orientation|screenSize" put this line in manifest file inside your MainActivity declaration.* nice example of [Million Monkeys Programming Style](https://en.wikipedia.org/wiki/Programming_by_permutation) – Selvin Sep 29 '17 at 07:58
  • @Selvin Could you post an example of what you would do? – user2070739 Sep 29 '17 at 07:58
  • initLoader in onCreate/ restartLoader in onOptionMenu ... remove all code connected to savedInstanceState ... with right Loader implementation it should work – Selvin Sep 29 '17 at 08:01

1 Answers1

1
Try this,

<activity
   android:name=".MainActivity"
   android:configChanges="orientation|screenSize"
  />
Pravin Fofariya
  • 346
  • 2
  • 11