2017-09-03 06:04:18 +00:00
|
|
|
package org.schabi.newpipe;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.os.Bundle;
|
2020-03-31 17:20:15 +00:00
|
|
|
import android.util.Log;
|
|
|
|
import android.view.View;
|
|
|
|
|
2019-10-04 12:59:08 +00:00
|
|
|
import androidx.annotation.NonNull;
|
2020-03-31 17:20:15 +00:00
|
|
|
import androidx.appcompat.app.AppCompatActivity;
|
2019-10-04 12:59:08 +00:00
|
|
|
import androidx.fragment.app.Fragment;
|
|
|
|
import androidx.fragment.app.FragmentManager;
|
2017-09-03 06:04:18 +00:00
|
|
|
|
|
|
|
import icepick.Icepick;
|
2018-08-24 09:54:41 +00:00
|
|
|
import icepick.State;
|
2020-05-01 15:16:40 +00:00
|
|
|
import leakcanary.AppWatcher;
|
2017-09-03 06:04:18 +00:00
|
|
|
|
|
|
|
public abstract class BaseFragment extends Fragment {
|
|
|
|
protected final String TAG = getClass().getSimpleName() + "@" + Integer.toHexString(hashCode());
|
2021-09-04 20:36:20 +00:00
|
|
|
protected static final boolean DEBUG = MainActivity.DEBUG;
|
2017-09-03 06:04:18 +00:00
|
|
|
protected AppCompatActivity activity;
|
2020-03-31 17:20:15 +00:00
|
|
|
//These values are used for controlling fragments when they are part of the frontpage
|
2018-08-24 09:54:41 +00:00
|
|
|
@State
|
|
|
|
protected boolean useAsFrontPage = false;
|
|
|
|
|
2020-03-31 17:20:15 +00:00
|
|
|
public void useAsFrontPage(final boolean value) {
|
2018-08-24 09:54:41 +00:00
|
|
|
useAsFrontPage = value;
|
|
|
|
}
|
|
|
|
|
2017-09-03 06:04:18 +00:00
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Fragment's Lifecycle
|
|
|
|
//////////////////////////////////////////////////////////////////////////*/
|
|
|
|
|
|
|
|
@Override
|
2020-11-01 12:55:20 +00:00
|
|
|
public void onAttach(@NonNull final Context context) {
|
2017-09-03 06:04:18 +00:00
|
|
|
super.onAttach(context);
|
|
|
|
activity = (AppCompatActivity) context;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDetach() {
|
|
|
|
super.onDetach();
|
|
|
|
activity = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-03-31 17:20:15 +00:00
|
|
|
public void onCreate(final Bundle savedInstanceState) {
|
|
|
|
if (DEBUG) {
|
|
|
|
Log.d(TAG, "onCreate() called with: "
|
|
|
|
+ "savedInstanceState = [" + savedInstanceState + "]");
|
|
|
|
}
|
2017-09-03 06:04:18 +00:00
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
Icepick.restoreInstanceState(this, savedInstanceState);
|
2020-03-31 17:20:15 +00:00
|
|
|
if (savedInstanceState != null) {
|
|
|
|
onRestoreInstanceState(savedInstanceState);
|
|
|
|
}
|
2017-09-03 06:04:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
2020-11-01 12:55:20 +00:00
|
|
|
public void onViewCreated(@NonNull final View rootView, final Bundle savedInstanceState) {
|
2017-09-03 06:04:18 +00:00
|
|
|
super.onViewCreated(rootView, savedInstanceState);
|
|
|
|
if (DEBUG) {
|
2020-03-31 17:20:15 +00:00
|
|
|
Log.d(TAG, "onViewCreated() called with: "
|
|
|
|
+ "rootView = [" + rootView + "], "
|
|
|
|
+ "savedInstanceState = [" + savedInstanceState + "]");
|
2017-09-03 06:04:18 +00:00
|
|
|
}
|
|
|
|
initViews(rootView, savedInstanceState);
|
|
|
|
initListeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-11-01 12:55:20 +00:00
|
|
|
public void onSaveInstanceState(@NonNull final Bundle outState) {
|
2017-09-03 06:04:18 +00:00
|
|
|
super.onSaveInstanceState(outState);
|
|
|
|
Icepick.saveInstanceState(this, outState);
|
|
|
|
}
|
|
|
|
|
2020-05-01 15:16:40 +00:00
|
|
|
protected void onRestoreInstanceState(@NonNull final Bundle savedInstanceState) {
|
|
|
|
}
|
2017-09-03 06:04:18 +00:00
|
|
|
|
2018-02-08 20:46:54 +00:00
|
|
|
@Override
|
|
|
|
public void onDestroy() {
|
|
|
|
super.onDestroy();
|
2018-02-10 19:07:17 +00:00
|
|
|
|
2020-05-01 15:16:40 +00:00
|
|
|
AppWatcher.INSTANCE.getObjectWatcher().watch(this);
|
2018-02-08 20:46:54 +00:00
|
|
|
}
|
|
|
|
|
2017-09-03 06:04:18 +00:00
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Init
|
|
|
|
//////////////////////////////////////////////////////////////////////////*/
|
|
|
|
|
2020-05-01 15:16:40 +00:00
|
|
|
protected void initViews(final View rootView, final Bundle savedInstanceState) {
|
|
|
|
}
|
2017-09-03 06:04:18 +00:00
|
|
|
|
2020-05-01 15:16:40 +00:00
|
|
|
protected void initListeners() {
|
|
|
|
}
|
2017-09-03 06:04:18 +00:00
|
|
|
|
2018-03-08 13:39:24 +00:00
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Utils
|
|
|
|
//////////////////////////////////////////////////////////////////////////*/
|
|
|
|
|
2020-03-31 17:20:15 +00:00
|
|
|
public void setTitle(final String title) {
|
|
|
|
if (DEBUG) {
|
|
|
|
Log.d(TAG, "setTitle() called with: title = [" + title + "]");
|
|
|
|
}
|
2021-10-16 19:33:45 +00:00
|
|
|
if (!useAsFrontPage && activity != null && activity.getSupportActionBar() != null) {
|
2019-10-20 00:31:17 +00:00
|
|
|
activity.getSupportActionBar().setDisplayShowTitleEnabled(true);
|
2018-03-08 13:39:24 +00:00
|
|
|
activity.getSupportActionBar().setTitle(title);
|
|
|
|
}
|
|
|
|
}
|
2018-06-19 20:40:43 +00:00
|
|
|
|
|
|
|
protected FragmentManager getFM() {
|
|
|
|
return getParentFragment() == null
|
|
|
|
? getFragmentManager()
|
|
|
|
: getParentFragment().getFragmentManager();
|
|
|
|
}
|
2017-09-03 06:04:18 +00:00
|
|
|
}
|