In the code below mSafeObserver.isDisposed(); always return null pointer exception if isDatabaseEmpty == false (zipped.subscribe(mSafeObserver); is not called).
private SafeObserver<ResponseHelper> mSafeObserver;
private Observer<ResponseHelper> observer = new Observer<ResponseHelper>() {
// ...
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mSafeObserver = new SafeObserver<>(observer);
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
// ...
if (isDatabaseEmpty) {
zipped.subscribe(mSafeObserver);
} else {
initRecycler();
}
}
#Override
public void onDestroy() {
super.onDestroy();
if (mSafeObserver != null && !mSafeObserver.isDisposed())
mSafeObserver.dispose();
}
In rxJava 1.0 this code is correct:
#Override
protected void onDestroy() {
super.onDestroy();
if (mSubscription != null && !mSubscription.isUnsubscribed())
mSubscription.unsubscribe();
}
How to check if I can dispose Disposable?
I don't know what zipped refers to but this how I would do it
private CompositeDisposable mDisposable = new CompositeDisposable();
private Observer<ResponseHelper> observer =
new Observer<ResponseHelper>() {
// ...
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mSafeObserver = new SafeObserver<>(observer);
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
// ...
if (isDatabaseEmpty) {
zipped
.doOnSubscribe(mDisposable::add)
.subscribe(mSafeObserver);
} else {
initRecycler();
}
}
#Override
public void onDestroy() {
super.onDestroy();
mDisposable.dispose();
}
Related
I am showing one dialog fragment (showing progress ) in my activity . Calling rest api methods are in my activity . Based on rest api results , I need to some progress in the dialog fragment . So I need to call a method of dialog fragment from activity . I tried with event bus , (Firing from Activity caught on Dialog Fragment ) - But events are not caught in dialog fragment . Is there any other solution ?
Fragment Code:
public class SyncProgressFragment extends BaseDialogFragment {
#BindView(R.id.layout_cancel)
LinearLayout layoutCancel;
#BindView(R.id.sync_with_master_breadcrumbs)
BreadcrumbsView syncWithMasterBreadCrumbs;
#BindView(R.id.master_breadcrumbs)
BreadcrumbsView masterBreadCrumbs;
#BindView(R.id.tvStep)
AppCompatTextView tvStep;
#BindView(R.id.tv_create_defect_title)
AppCompatTextView tvTitle;
private ThreadBus bus;
private FragmentManager manager;
private GoogleApiHelper googleApiHelper;
private Handler handler;
private Runnable runnable;
private boolean isOld;
private boolean isSync;
//private long requestId;
private BreadcrumbsView breadcrumbsView;
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
bus = AppController.getInstance().getBus();
bus.register(this);
googleApiHelper = AppController.getInstance().getGoogleApiHelper();
googleApiHelper.reconnect();
if (getArguments() != null) {
Bundle bundle = getArguments();
isOld = bundle.getBoolean(AppConstants.IS_OLD,false);
isSync = bundle.getBoolean(AppConstants.IS_OLD,false);
}
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
initUI();
}
private void initUI() {
if (isOld && isSync)
tvTitle.setText(getString(R.string.syncing_old_data_with_bo));
else if(!isOld && isSync)
tvTitle.setText(getString(R.string.syncing_data_with_bo));
else {
tvTitle.setText(getString(R.string.downloading_master_data));
}
if(isSync) {
breadcrumbsView = syncWithMasterBreadCrumbs;
syncWithMasterBreadCrumbs.setVisibility(View.VISIBLE);
masterBreadCrumbs.setVisibility(View.GONE);
tvStep.setText(getString(R.string.sending_data));
}
else{
breadcrumbsView = masterBreadCrumbs;
masterBreadCrumbs.setVisibility(View.VISIBLE);
syncWithMasterBreadCrumbs.setVisibility(View.GONE);
tvStep.setText(getString(R.string.getting_master_data));
}
}
private void initValues() {
handler = new Handler();
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
// request a window without the title
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
dialog.setCanceledOnTouchOutside(false);
dialog.setCancelable(false);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
return dialog;
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_sync_progress, container, false);
ButterKnife.bind(this, view);
initValues();
return view;
}
#Override
public void onStart() {
super.onStart();
}
#Override
public void onResume() {
super.onResume();
}
#Override
public void onPause() {
super.onPause();
}
#Override
public void onStop() {
super.onStop();
}
#Override
public void onDestroy() {
super.onDestroy();
if (bus != null) {
bus.unregister(this);
}
try {
handler.removeCallbacks(runnable);
} catch (Exception e) {
}
}
#Override
public void onDestroyView() {
super.onDestroyView();
}
#Override
public void setCancelable(boolean cancelable) {
super.setCancelable(cancelable);
}
#Override
public void onCancel(DialogInterface dialog) {
super.onCancel(dialog);
}
#Override
public void onDismiss(DialogInterface dialog) {
super.onDismiss(dialog);
try {
handler.removeCallbacks(runnable);
} catch (Exception e) {
}
}
#OnClick({R.id.img_close, R.id.layout_cancel})
public void onViewClicked(View view) {
switch (view.getId()) {
case R.id.layout_cancel:
case R.id.img_close:
dismissAllowingStateLoss();
break;
}
}
#Subscribe
public void onSyncGotResponse(SyncGotResponse syncGotResponse) {
Utilities.log(SyncProgressFragment.class,"onSyncGotResponse",""+syncGotResponse.isSuccess());
if(syncGotResponse.isSuccess()) {
breadcrumbsView.nextStep();
tvStep.setText(getString(R.string.synced_data_with_bo));
}
else
dismissAllowingStateLoss();
}
#Subscribe
public void onMasterGotResponse(MasterGotResponse masterGotResponse) {
Utilities.log(SyncProgressFragment.class,"onMasterGotResponse",""+masterGotResponse.isSuccess());
if(masterGotResponse.isSuccess()) {
tvStep.setText(getString(R.string.downloaded_data_from_bo));
breadcrumbsView.nextStep();
}
else
dismissAllowingStateLoss();
}
}
Activity Code:
#Override
public void dataSyncSuccess(DataSyncResponse dataSyncResponse) {
bus.post(new SyncGotResponse(true));
}
#Override
public void dataSyncFailure(String msg) {
UiUtils.showToast(getContext(), msg);
bus.post(new SyncGotResponse(false));
}
//Calling Rest Api method
if (CheckInternetConnection(getContext())) {
try {
presenter.data_sync();
Bundle bundle = new Bundle();
bundle.putBoolean(AppConstants.IS_OLD,false);
bundle.putBoolean(AppConstants.IS_SYNC,true);
syncProgressFragment.setArguments(bundle);
syncProgressFragment.setCancelable(false);
syncProgressFragment.show(getSupportFragmentManager(), syncProgressFragment.getTag());
} catch (JSONException e) {
e.printStackTrace();
}
} else {
ShowSanckBarShow();
}
I'm making an authenticator following the tutorial: http://blog.udinic.com/2013/04/24/write-your-own-android-authenticator/
The login Activity requires to extend AccountAuthenticatorActivity, the issue starts here: AccountAuthenticatorActivity extends the regular Activity and not AppCompatActivity.
Using the regular Activity in AppCompat results in a Activity without ActionBar. I want to use AccountAuthenticatorActivity AND having an ActionBar.
I think that is not the real solution. If you are doing an app with support libraries, mixing AppCompatActivities, Fragments &c with the standard ones is not a good idea.
I´ve created an AccountAuthenticatorAppCompatActivity extending AppCompatActivity and then copy/paste the code from API AccountAuthenticatorActivity and it seems to work properly.
public class AccountAuthenticatorAppCompatActivity extends AppCompatActivity {
private AccountAuthenticatorResponse mAccountAuthenticatorResponse = null;
private Bundle mResultBundle = null;
public final void setAccountAuthenticatorResult(Bundle result) {
mResultBundle = result;
}
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
mAccountAuthenticatorResponse =
getIntent().getParcelableExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE);
if (mAccountAuthenticatorResponse != null) {
mAccountAuthenticatorResponse.onRequestContinued();
}
}
public void finish() {
if (mAccountAuthenticatorResponse != null) {
// send the result bundle back if set, otherwise send an error.
if (mResultBundle != null) {
mAccountAuthenticatorResponse.onResult(mResultBundle);
} else {
mAccountAuthenticatorResponse.onError(AccountManager.ERROR_CODE_CANCELED,
"canceled");
}
mAccountAuthenticatorResponse = null;
}
super.finish();
}
}
Hope it helps to someone.
The key is AppCompatDelegate, my code is based on the AppCompatPreferenceActivity class generated by Android Studio:
#SuppressWarnings("unused")
public class AppCompatAuthActivity extends AccountAuthenticatorActivity {
private AppCompatDelegate mDelegate;
#Override
protected void onCreate(Bundle savedInstanceState) {
getDelegate().installViewFactory();
getDelegate().onCreate(savedInstanceState);
super.onCreate(savedInstanceState);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
getDelegate().onPostCreate(savedInstanceState);
}
public ActionBar getSupportActionBar() {
return getDelegate().getSupportActionBar();
}
public void setSupportActionBar(#Nullable Toolbar toolbar) {
getDelegate().setSupportActionBar(toolbar);
}
#Override
#NonNull
public MenuInflater getMenuInflater() {
return getDelegate().getMenuInflater();
}
#Override
public void setContentView(#LayoutRes int layoutResID) {
getDelegate().setContentView(layoutResID);
}
#Override
public void setContentView(View view) {
getDelegate().setContentView(view);
}
#Override
public void setContentView(View view, ViewGroup.LayoutParams params) {
getDelegate().setContentView(view, params);
}
#Override
public void addContentView(View view, ViewGroup.LayoutParams params) {
getDelegate().addContentView(view, params);
}
#Override
protected void onPostResume() {
super.onPostResume();
getDelegate().onPostResume();
}
#Override
protected void onTitleChanged(CharSequence title, int color) {
super.onTitleChanged(title, color);
getDelegate().setTitle(title);
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
getDelegate().onConfigurationChanged(newConfig);
}
#Override
protected void onStop() {
super.onStop();
getDelegate().onStop();
}
#Override
protected void onDestroy() {
super.onDestroy();
getDelegate().onDestroy();
}
public void invalidateOptionsMenu() {
getDelegate().invalidateOptionsMenu();
}
private AppCompatDelegate getDelegate() {
if (mDelegate == null) {
mDelegate = AppCompatDelegate.create(this, null);
}
return mDelegate;
}
}
The AppCompatDelegate is the key to add ActionBar to ANY regular Activity (for example PreferenceActivity).
Don't forget your activity must extend AppCompatAuthActivity.
I'm buliding a simple android app, and i got this error when i try to send data to API. I use RxJava and Retrofit, and I use Model View Presenter.
I got this error "btnservice is a null object reference"
I always got btnservice null, please help to solve this.
Thank you
This the JSON
request:
{
name: '',
mobile: '',
email: ''
}
This my BaseActivtiy
public abstract class BaseActivity extends AppCompatActivity {
#BindString(R.string.loading)
public String loading;
#BindInt(R.integer.success_code)
public int successCode;
#BindInt(R.integer.success_activication_code)
public int activicationSuccessCode;
protected BTNService btnService;
protected abstract int getLayout();
private ProgressDialog progressDialog;
private CompositeSubscription subscriptions;
protected RxBus bus;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayout());
Icepick.restoreInstanceState(this, savedInstanceState);
ButterKnife.bind(this);
this.progressDialog = new ProgressDialog(this);
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Icepick.saveInstanceState(this, outState);
}
#Override
protected void onStart() {
super.onStart();
this.subscriptions = new CompositeSubscription();
}
#Override
public void onStop() {
super.onStop();
}
#Override
protected void onDestroy() {
super.onDestroy();
this.progressDialog.dismiss();
}
public BTNService getBTNService() {
return btnService;
}
public RxBus getBus() {
return bus;
}
public void showProgressDialog(String message) {
if (progressDialog != null) {
progressDialog.setMessage(message);
progressDialog.show();
}
}
public void dismissProgressDialog() {
progressDialog.hide();
}
public boolean isFragmentNotNull(String tag) {
if (getSupportFragmentManager().findFragmentByTag(tag) != null) {
return true;
} else {
return false;
}
}
public boolean isFragmentVisible(String tag) {
if (isFragmentNotNull(tag)
&& getSupportFragmentManager().findFragmentByTag(tag).isVisible()) {
return true;
} else {
return false;
}
}
}
This my BaseFragment
public abstract class BaseFragment extends Fragment implements Validator.ValidationListener {
#BindString(R.string.loading)
public String loading;
#BindInt(R.integer.success_code)
public int successCode;
#BindInt(R.integer.success_activication_code)
public int activicationSuccessCode;
#BindString(R.string.connection_error)
public String connectionError;
protected abstract int getLayout();
protected Validator validator;
private CompositeSubscription subscriptions;
protected RxBus bus;
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
this.bus = ((BaseActivity) getActivity()).getBus();
}
#Override
public void onStart() {
super.onStart();
this.subscriptions = new CompositeSubscription();
/* this.subscriptions
.add(bus.toObserverable()
.subscribe(new Action1<Object>() {
#Override
public void call(Object event) {
if (event instanceof RxBusObject) {
RxBusObject busObject = (RxBusObject) event;
busHandler();
}
}
})
);*/
}
#Override
public void onStop() {
super.onStop();
this.subscriptions.clear();
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(getLayout(), container, false);
ButterKnife.bind(this, view);
return view;
}
public RxBus getBus() {
return this.bus;
}
#Override
public void onValidationSucceeded() {
}
#Override
public void onValidationFailed(List<ValidationError> errors) {
for (ValidationError error : errors) {
View view = error.getView();
String message = error.getCollatedErrorMessage(getActivity());
if (view instanceof EditText) {
EditText et = ((EditText) view);
et.setError(message);
et.requestFocus();
} else {
Toast.makeText(getActivity(), message, Toast.LENGTH_LONG).show();
}
}
}
public void showSuccessDialog(String message, final Boolean isFinishActivity) {
new MaterialDialog.Builder(getActivity())
.iconRes(R.mipmap.ic_launcher)
.title(getString(R.string.success).toUpperCase())
.titleColor(Color.WHITE)
.content(message)
.contentColor(Color.WHITE)
.positiveText(R.string.ok)
.positiveColor(Color.WHITE)
.onPositive(new MaterialDialog.SingleButtonCallback() {
#Override
public void onClick(MaterialDialog dialog, DialogAction which) {
if (isFinishActivity) {
getActivity().finish();
}
}
})
.cancelable(false)
.show();
}
public void showProgressDialog(String message) {
((BaseActivity) getActivity()).showProgressDialog(message);
}
public Validator getValidator() {
return validator;
}
public BTNService.Api getApi() {
return ((BaseActivity) getActivity()).getBTNService().getApi();
}
public void dismissProgressDialog() {
((BaseActivity) getActivity()).dismissProgressDialog();
}
}
This my Retrofit Class
public BTNService(Context context) {
if (retrofit==null) {
Retrofit retrofit = new Retrofit.Builder()
.client(provideOkHttpClient(context))
.baseUrl(BASE_URL)
.build();
this.api = retrofit.create(Api.class);
}
}
private OkHttpClient provideOkHttpClient(final Context context) {
HttpLoggingInterceptor httpLoggingInterceptorinterceptor = new HttpLoggingInterceptor();
httpLoggingInterceptorinterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
httpClient.addInterceptor(httpLoggingInterceptorinterceptor);
httpClient.addInterceptor(new Interceptor() {
#Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Response response = chain.proceed(request);
return response;
}
});
return httpClient.build();
}
public Api getApi() {
return api;
}
public interface Api {
#POST(PORTAL_URL + "customer/register")
Observable<SignUpResponse> regsiterCustomer(#Path("email") String Email,
#Path("name") String Name,
#Path("mobile") String PhoneNumber);
}
and this my Presenter
public class SignUpPresenter {
private SignUpFragment fragment;
public SignUpPresenter(SignUpFragment fragment) {
this.fragment = fragment;
}
public SignUpRequest constructSignUpRequest() {
SignUpRequest request = new SignUpRequest();
request.setName(getAndTrimValueFromEditText(fragment.etName));
request.setEmail(getAndTrimValueFromEditText(fragment.etEmail));
request.setMobile(getAndTrimValueFromEditText(fragment.etPhone));
return request;
}
private String getAndTrimValueFromEditText(EditText e) {
return e.getText().toString().trim();
}
void signup (){
this.register(constructSignUpRequest());
}
void register(final SignUpRequest signUpRequest) {
fragment.showProgressDialog(fragment.loading);
fragment.getApi().regsiterCustomer(
signUpRequest.getName(),
signUpRequest.getEmail(),
signUpRequest.getMobile())
.subscribeOn(Schedulers.io())
.subscribe(new Observer<GenericResponse>() {
#Override
public void onCompleted() {
}
#Override
public void onError(Throwable e) {
fragment.dismissProgressDialog();
Timber.e(e.getLocalizedMessage());
Toast.makeText(fragment.getContext(), fragment.connectionError, Toast.LENGTH_SHORT).show();
}
#Override
public void onNext(GenericResponse genericResponse) {
fragment.dismissProgressDialog();
Toast.makeText(fragment.getContext(), genericResponse.getInfo(), Toast.LENGTH_SHORT).show();
if (genericResponse.getCode() == fragment.successCode) {
/*fragment.gotoActivationCodeActivity(SignUpRequest.getEmail(), SignUpRequest.get());*/
fragment.gotoQuestionActivity(signUpRequest.getEmail(), signUpRequest.getEmail(), signUpRequest.getMobile());
}
}
});
}
public static <T> Function<BaseResponse<T>, Observable<T>> convertDataFlatMap() {
return new Function<BaseResponse<T>, Observable<T>>() {
#Override
public Observable<T> apply(BaseResponse<T> response) {
if (!Constants.SUCCESS_CODE.equals(response.getStatus_code())) {
BaseErrorResponse baseErrorResponse = new BaseErrorResponse();
baseErrorResponse.setError(response.getStatus_code(),
response.getStatus());
return Observable.error(BaseException.toServerError(baseErrorResponse));
}
T data = response.getData();
if (null == data) {
return Observable.empty();
}
return Observable.just(response.getData());
}
};
}
I'm using Rxjava 2, Maybe useful for you.
I have parent fragment called OpFragment. From this fragment are inherited all fragments in my app.
public abstract class OpFragment extends Fragment {
private Loading loading;
public abstract int getLayoutId();
public abstract void getData();
public abstract void setListeners();
protected BackHandlerInterface backHandlerInterface;
public boolean onBackPressed(){
return false;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
if(!(getActivity() instanceof BackHandlerInterface)) {
throw new ClassCastException("Hosting activity must implement BackHandlerInterface");
} else {
backHandlerInterface = (BackHandlerInterface) getActivity();
}
FragmentArgs.inject(this);
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(getLayoutId(), container, false);
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
if(!isAdded()){
return;
}
getData();
setListeners();
}
protected String returnName() {
return null;
}
public void showTitle() {
EventBus.getDefault().post(new ShowName(returnName()));
}
#Override
public void onDestroyView() {
super.onDestroyView();
}
public Loading getLoading() {
if (this.loading == null) {
this.loading = new Loading(this.getActivity());
}
return this.loading;
}
/**
* Gets a component for dependency injection by its type.
*/
#SuppressWarnings("unchecked")
protected <C> C getComponent(Class<C> componentType) {
return componentType.cast(((HasComponent<C>) getActivity()).getComponent());
}
#Override
public void onStart() {
super.onStart();
backHandlerInterface.setSelectedFragment(this);
}
public interface BackHandlerInterface {
void setSelectedFragment(OpFragment backHandledFragment);
}
#Override
public void onResume() {
super.onResume();
sendGAScreens();
}
private void sendGAScreens() {
final Tracker tracker = OpApp.getDefaultTracker();
if(tracker != null) {
tracker.setScreenName(getClass().getSimpleName());
tracker.send(new HitBuilders.ScreenViewBuilder().build());
}
}
}
There are methods getData() and setListeners() inside onViewCreated.
I don't want to recall this methods after screen rotation. How can I do that ?
Simply checking savedInstanceState == null not gave me expected result.
override this method to detect screen rotation in your fragment and set some flag if its screen rotation. as shown below:
#Override
public void onConfigurationChanged(Configuration newConfig)
{
Log.d("tag", "config changed");
super.onConfigurationChanged(newConfig);
int orientation = newConfig.orientation;
if (orientation == Configuration.ORIENTATION_PORTRAIT || orientation == Configuration.ORIENTATION_LANDSCAPE)
flag= true;
....
}
and in your onViewCreated() do like this
if(!flag){
// call your functions
}
I'm experiencing an strange behavior of Fragment life-cycle each time that i rotate the screen. Only the first half of life-cycle methods are getting called, onPause,onSaveInstanceState,onStop, onDestroyView,onDestroy and onDetach. The other half (onAttach ...-onResume) are not getting called. The Activity associated with the Fragment is calling all its life-cycle methods.
Any help would be highly appreciated because I'm stuck on this issue.
Thanks in advance.
Here the entire code of the Activity and the static nested class where is the Fragment:
public class MoviesFeed extends AppCompatActivity {
private static boolean mTwoPane;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("ACT","onCreate");
setContentView(R.layout.themoviedb_main);
if(findViewById(R.id.detail_activity_container)!=null) {
//the detail_activity_container will be present only in large-screen
//Layouts (res/Layout-sw600dp. If this view is present, then the activity
//should be in two-pane mode
mTwoPane=true;
//In two-pane mode, show the detail view in this activity by adding
// or replacing the detail fragment using a fragment transaction
DetailActivityFragment detailActivityFragment=new DetailActivityFragment();
// Bundle bundle=new Bundle();
// bundle.putBoolean("twopane",mTwoPane);
// detailActivityFragment.setArguments(bundle);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.detail_activity_container, detailActivityFragment)
.commit();
}
} else {
mTwoPane=false;
}
}
#Override
protected void onStart() {
Log.d("ACT","onStart");
super.onStart();
}
#Override
protected void onResume() {
Log.d("ACT","onResume");
super.onResume();
}
#Override
protected void onPause() {
Log.d("ACT","onPause");
super.onPause();
}
#Override
protected void onStop() {
Log.d("ACT","onStop");
super.onStop();
}
#Override
protected void onRestart() {
Log.d("ACT","onRestart");
super.onRestart();
}
#Override
protected void onDestroy() {
Log.d("ACT","onDestroy");
super.onDestroy();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.movies_feed_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
startActivity(new Intent(this,SettingsActivity.class));
return true;
}
return super.onOptionsItemSelected(item);
}
public static class MoviesFeedFragment extends Fragment implements AdapterView.OnItemClickListener {
private static final int APPROX_FIXED_IMAGE_WIDTH=170;
private GridView mGridView;
private MovieAdapter mMovieAdapter;
private ArrayList<Response.Movie> mListMovies=new ArrayList<Response.Movie>();
private TimeMeasure mTm;
private boolean mFromDetailsActivity =false;
private boolean mUserRotation=false;
private boolean mFavoritesMode=false;
#Override
public void onAttach(Activity activity) {
Log.d("FRAG", "onAttach");
super.onAttach(activity);
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("FRAG", "onCreate");
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.fragment_moviesfeed, container, false);
Log.d("FRAG", "onCreateView");
mGridView= (GridView) view.findViewById(R.id.gridView);
mGridView.setOnItemClickListener(this);
mMovieAdapter = new MovieAdapter(getActivity(), mListMovies);
mGridView.setAdapter(mMovieAdapter);
//for tablets specially
// float scalefactor = getResources().getDisplayMetrics().density * APPROX_FIXED_IMAGE_WIDTH;
// Point size=new Point();
// getWindowManager().getDefaultDisplay().getSize(size);
// int number=size.x;
// int columns = (int) ((float) number / (float) scalefactor);
//
// mGridView.setNumColumns(columns);
if(savedInstanceState!=null){
mUserRotation=true;
ArrayList<Response.Movie> tempList=new ArrayList<Response.Movie>();
tempList=savedInstanceState.getParcelableArrayList("mListMovies");
mListMovies.clear();
mListMovies.addAll(tempList);
mMovieAdapter = new MovieAdapter(getActivity(), mListMovies);
mGridView.setAdapter(mMovieAdapter);
}
return view;
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
Log.d("FRAG", "onActivity");
super.onActivityCreated(savedInstanceState);
}
#Override
public void onPause() {
Log.d("FRAG", "onPause");
super.onPause();
}
#Override
public void onStop() {
Log.d("FRAG", "onStop");
super.onStop();
}
#Override
public void onSaveInstanceState(Bundle outState) {
Log.d("FRAG", "onSaveInstanceState");
super.onSaveInstanceState(outState);
outState.putParcelableArrayList("mListMovies", mListMovies);
}
#Override
public void onResume() {
Log.d("FRAG", "onResume");
super.onResume();
if (mFromDetailsActivity !=true && mUserRotation!=true) {
executeCallToMoviesApi();
} else if(mFromDetailsActivity==true && mFavoritesMode==true) {
getFavoritesMovies();
}
mFromDetailsActivity =false;
mUserRotation=false;
}
#Override
public void onDestroyView() {
Log.d("FRAG", "onDestroyView");
super.onDestroyView();
}
#Override
public void onDestroy() {
Log.d("FRAG", "onDestroy");
super.onDestroy();
}
#Override
public void onDetach() {
Log.d("FRAG", "onDetach");
super.onDetach();
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (mTwoPane==true) {
DetailActivityFragment detailActivityFragment=new DetailActivityFragment();
Bundle args=new Bundle();
args.putString("movieId", String.valueOf(mListMovies.get(position).getId()));
//Response.Movie movie=new Response.Movie();
//movie.setId(mListMovies.get(position).getId());
//args.putParcelable("movie",movie);
args.putBoolean("favoritesMode",mFavoritesMode);
detailActivityFragment.setArguments(args);
getActivity().getSupportFragmentManager().beginTransaction()
.replace(R.id.detail_activity_container,detailActivityFragment)
.commit();
} else {
Intent intent = new Intent(getActivity(), DetailActivity.class);
intent.putExtra("favoritesMode", mFavoritesMode);
intent.putExtra("movieId", mListMovies.get(position).getId());
mFromDetailsActivity = true;
startActivity(intent);
}
}
public void executeCallToMoviesApi(){
SharedPreferences sharedPreferences= PreferenceManager.getDefaultSharedPreferences(getActivity());
String orderStr= sharedPreferences.getString(getString(R.string.pref_order_key),
getString(R.string.pref_order_default));
mFavoritesMode=false;
if (orderStr.equals(getString(R.string.pref_popularity))){
getActivity().setTitle(getString(R.string.mainactivity_title_popularity));
getMoviesByPopularity();
}
if (orderStr.equals(getString(R.string.pref_rate))){
getActivity().setTitle(getString(R.string.mainactivity_title_rate));
getMoviesByRate();
}
if (orderStr.equals(getString(R.string.pref_favorites))) {
getActivity().setTitle(getString(R.string.mainactivity_title_favorites));
mFavoritesMode=true;
getFavoritesMovies();
}
}
public void getMoviesByPopularity(){
ApiClient.MyApi myApi=ApiClient.getMyApiClient();
myApi.getMoviesByPopularityDesc(AppConstants.API_KEY, callbackResponse());
}
public void getMoviesByRate(){
ApiClient.MyApi myApi=ApiClient.getMyApiClient();
myApi.getMoviesByAverageRate(AppConstants.API_KEY, callbackResponse());
}
private Callback<Response> callbackResponse() {
return new Callback<Response>() {
#Override
public void success(Response response, retrofit.client.Response response2) {
// Message.displayToast(MoviesFeed.this, "success");
mListMovies.clear();
mListMovies.addAll((ArrayList) response.getResults());
mMovieAdapter = new MovieAdapter(getActivity(), mListMovies);
mGridView.setAdapter(mMovieAdapter);
}
#Override
public void failure(RetrofitError error) {
Log.v("VILLANUEVA", "error:" + error.getMessage().toString());
Message.displayToast(getActivity(), "failure" + error.getMessage().toString());
}
};
}
public void getFavoritesMovies(){
List<MovieDetail> tempListDetail;
ArrayList<Response.Movie> tempList=new ArrayList<Response.Movie>();
SharedPreferenceManager sharedPreferenceManager=new SharedPreferenceManager(getActivity());
tempListDetail = sharedPreferenceManager.getFavoritesList();
Response.Movie tempMovie;
if (tempListDetail!=null) {
for (MovieDetail movieDetail : tempListDetail) {
tempMovie = new Response.Movie();
tempMovie.setId(movieDetail.getId());
tempMovie.setPoster_path(movieDetail.getPoster_path());
tempList.add(tempMovie);
}
mListMovies.clear();
mListMovies.addAll(tempList);
}
mMovieAdapter = new MovieAdapter(getActivity(), mListMovies);
mGridView.setAdapter(mMovieAdapter);
}
}//MoviesFeedFragment
Screenshots, before and after rotation.
Before:
After:
Log