2017-09-03 08:04:18 +02:00
|
|
|
package org.schabi.newpipe;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.os.Bundle;
|
2020-03-31 19:20:15 +02:00
|
|
|
import android.util.Log;
|
|
|
|
import android.view.View;
|
|
|
|
|
2019-10-04 14:59:08 +02:00
|
|
|
import androidx.annotation.NonNull;
|
2020-03-31 19:20:15 +02:00
|
|
|
import androidx.appcompat.app.AppCompatActivity;
|
2019-10-04 14:59:08 +02:00
|
|
|
import androidx.fragment.app.Fragment;
|
|
|
|
import androidx.fragment.app.FragmentManager;
|
2017-09-03 08:04:18 +02:00
|
|
|
|
|
|
|
import icepick.Icepick;
|
2018-08-24 11:54:41 +02:00
|
|
|
import icepick.State;
|
2017-09-03 08:04:18 +02:00
|
|
|
|
|
|
|
public abstract class BaseFragment extends Fragment {
|
|
|
|
protected final String TAG = getClass().getSimpleName() + "@" + Integer.toHexString(hashCode());
|
2021-09-04 22:36:20 +02:00
|
|
|
protected static final boolean DEBUG = MainActivity.DEBUG;
|
2017-09-03 08:04:18 +02:00
|
|
|
protected AppCompatActivity activity;
|
2020-03-31 19:20:15 +02:00
|
|
|
//These values are used for controlling fragments when they are part of the frontpage
|
2018-08-24 11:54:41 +02:00
|
|
|
@State
|
|
|
|
protected boolean useAsFrontPage = false;
|
|
|
|
|
2020-03-31 19:20:15 +02:00
|
|
|
public void useAsFrontPage(final boolean value) {
|
2018-08-24 11:54:41 +02:00
|
|
|
useAsFrontPage = value;
|
|
|
|
}
|
|
|
|
|
2017-09-03 08:04:18 +02:00
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Fragment's Lifecycle
|
|
|
|
//////////////////////////////////////////////////////////////////////////*/
|
|
|
|
|
|
|
|
@Override
|
2020-11-01 13:55:20 +01:00
|
|
|
public void onAttach(@NonNull final Context context) {
|
2017-09-03 08:04:18 +02:00
|
|
|
super.onAttach(context);
|
|
|
|
activity = (AppCompatActivity) context;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDetach() {
|
|
|
|
super.onDetach();
|
|
|
|
activity = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-03-31 19:20:15 +02:00
|
|
|
public void onCreate(final Bundle savedInstanceState) {
|
|
|
|
if (DEBUG) {
|
|
|
|
Log.d(TAG, "onCreate() called with: "
|
|
|
|
+ "savedInstanceState = [" + savedInstanceState + "]");
|
|
|
|
}
|
2017-09-03 08:04:18 +02:00
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
Icepick.restoreInstanceState(this, savedInstanceState);
|
2020-03-31 19:20:15 +02:00
|
|
|
if (savedInstanceState != null) {
|
|
|
|
onRestoreInstanceState(savedInstanceState);
|
|
|
|
}
|
2017-09-03 08:04:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
2020-11-01 13:55:20 +01:00
|
|
|
public void onViewCreated(@NonNull final View rootView, final Bundle savedInstanceState) {
|
2017-09-03 08:04:18 +02:00
|
|
|
super.onViewCreated(rootView, savedInstanceState);
|
|
|
|
if (DEBUG) {
|
2020-03-31 19:20:15 +02:00
|
|
|
Log.d(TAG, "onViewCreated() called with: "
|
|
|
|
+ "rootView = [" + rootView + "], "
|
|
|
|
+ "savedInstanceState = [" + savedInstanceState + "]");
|
2017-09-03 08:04:18 +02:00
|
|
|
}
|
|
|
|
initViews(rootView, savedInstanceState);
|
|
|
|
initListeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-11-01 13:55:20 +01:00
|
|
|
public void onSaveInstanceState(@NonNull final Bundle outState) {
|
2017-09-03 08:04:18 +02:00
|
|
|
super.onSaveInstanceState(outState);
|
|
|
|
Icepick.saveInstanceState(this, outState);
|
|
|
|
}
|
|
|
|
|
2020-05-01 17:16:40 +02:00
|
|
|
protected void onRestoreInstanceState(@NonNull final Bundle savedInstanceState) {
|
|
|
|
}
|
2017-09-03 08:04:18 +02:00
|
|
|
|
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Init
|
|
|
|
//////////////////////////////////////////////////////////////////////////*/
|
|
|
|
|
2023-08-29 15:33:03 +02:00
|
|
|
/**
|
|
|
|
* This method is called in {@link #onViewCreated(View, Bundle)} to initialize the views.
|
|
|
|
*
|
|
|
|
* <p>
|
|
|
|
* {@link #initListeners()} is called after this method to initialize the corresponding
|
|
|
|
* listeners.
|
|
|
|
* </p>
|
|
|
|
* @param rootView The inflated view for this fragment
|
|
|
|
* (provided by {@link #onViewCreated(View, Bundle)})
|
|
|
|
* @param savedInstanceState The saved state of this fragment
|
|
|
|
* (provided by {@link #onViewCreated(View, Bundle)})
|
|
|
|
*/
|
2020-05-01 17:16:40 +02:00
|
|
|
protected void initViews(final View rootView, final Bundle savedInstanceState) {
|
|
|
|
}
|
2017-09-03 08:04:18 +02:00
|
|
|
|
2023-08-29 15:33:03 +02:00
|
|
|
/**
|
|
|
|
* Initialize the listeners for this fragment.
|
|
|
|
*
|
|
|
|
* <p>
|
|
|
|
* This method is called after {@link #initViews(View, Bundle)}
|
|
|
|
* in {@link #onViewCreated(View, Bundle)}.
|
|
|
|
* </p>
|
|
|
|
*/
|
2020-05-01 17:16:40 +02:00
|
|
|
protected void initListeners() {
|
|
|
|
}
|
2017-09-03 08:04:18 +02:00
|
|
|
|
2018-03-08 14:39:24 +01:00
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Utils
|
|
|
|
//////////////////////////////////////////////////////////////////////////*/
|
|
|
|
|
2020-03-31 19:20:15 +02:00
|
|
|
public void setTitle(final String title) {
|
|
|
|
if (DEBUG) {
|
|
|
|
Log.d(TAG, "setTitle() called with: title = [" + title + "]");
|
|
|
|
}
|
2021-10-16 21:33:45 +02:00
|
|
|
if (!useAsFrontPage && activity != null && activity.getSupportActionBar() != null) {
|
2019-10-20 02:31:17 +02:00
|
|
|
activity.getSupportActionBar().setDisplayShowTitleEnabled(true);
|
2018-03-08 14:39:24 +01:00
|
|
|
activity.getSupportActionBar().setTitle(title);
|
|
|
|
}
|
|
|
|
}
|
2018-06-19 22:40:43 +02:00
|
|
|
|
2023-12-10 16:06:07 +01:00
|
|
|
/**
|
|
|
|
* Finds the root fragment by looping through all of the parent fragments. The root fragment
|
|
|
|
* is supposed to be {@link org.schabi.newpipe.fragments.MainFragment}, and is the fragment that
|
|
|
|
* handles keeping the backstack of opened fragments in NewPipe, and also the player bottom
|
|
|
|
* sheet. This function therefore returns the fragment manager of said fragment.
|
|
|
|
*
|
|
|
|
* @return the fragment manager of the root fragment, i.e.
|
|
|
|
* {@link org.schabi.newpipe.fragments.MainFragment}
|
|
|
|
*/
|
2018-06-19 22:40:43 +02:00
|
|
|
protected FragmentManager getFM() {
|
2023-12-10 16:06:07 +01:00
|
|
|
Fragment current = this;
|
|
|
|
while (current.getParentFragment() != null) {
|
|
|
|
current = current.getParentFragment();
|
|
|
|
}
|
|
|
|
return current.getFragmentManager();
|
2018-06-19 22:40:43 +02:00
|
|
|
}
|
2017-09-03 08:04:18 +02:00
|
|
|
}
|