mirror of
https://github.com/TeamNewPipe/NewPipe
synced 2025-01-24 07:56:57 +00:00
Fixed problems with Android's lifecycle (restore)
This commit is contained in:
parent
8bbc3e531c
commit
ce4dd33eab
@ -3,6 +3,7 @@ package org.schabi.newpipe.settings;
|
|||||||
import static org.schabi.newpipe.util.Localization.assureCorrectAppLanguage;
|
import static org.schabi.newpipe.util.Localization.assureCorrectAppLanguage;
|
||||||
|
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.text.TextUtils;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.view.Menu;
|
import android.view.Menu;
|
||||||
import android.view.MenuItem;
|
import android.view.MenuItem;
|
||||||
@ -40,6 +41,9 @@ import org.schabi.newpipe.views.FocusOverlayView;
|
|||||||
|
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
import icepick.Icepick;
|
||||||
|
import icepick.State;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Created by Christian Schabesberger on 31.08.15.
|
* Created by Christian Schabesberger on 31.08.15.
|
||||||
*
|
*
|
||||||
@ -77,20 +81,37 @@ public class SettingsActivity extends AppCompatActivity implements
|
|||||||
private View searchContainer;
|
private View searchContainer;
|
||||||
private EditText searchEditText;
|
private EditText searchEditText;
|
||||||
|
|
||||||
|
// State
|
||||||
|
@State
|
||||||
|
String searchText;
|
||||||
|
@State
|
||||||
|
boolean wasSearchActive;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(final Bundle savedInstanceBundle) {
|
protected void onCreate(final Bundle savedInstanceBundle) {
|
||||||
setTheme(ThemeHelper.getSettingsThemeStyle(this));
|
setTheme(ThemeHelper.getSettingsThemeStyle(this));
|
||||||
assureCorrectAppLanguage(this);
|
assureCorrectAppLanguage(this);
|
||||||
|
|
||||||
super.onCreate(savedInstanceBundle);
|
super.onCreate(savedInstanceBundle);
|
||||||
|
Icepick.restoreInstanceState(this, savedInstanceBundle);
|
||||||
|
final boolean restored = savedInstanceBundle != null;
|
||||||
|
|
||||||
final SettingsLayoutBinding settingsLayoutBinding =
|
final SettingsLayoutBinding settingsLayoutBinding =
|
||||||
SettingsLayoutBinding.inflate(getLayoutInflater());
|
SettingsLayoutBinding.inflate(getLayoutInflater());
|
||||||
setContentView(settingsLayoutBinding.getRoot());
|
setContentView(settingsLayoutBinding.getRoot());
|
||||||
initSearch(settingsLayoutBinding);
|
initSearch(settingsLayoutBinding, restored);
|
||||||
|
|
||||||
setSupportActionBar(settingsLayoutBinding.settingsToolbarLayout.toolbar);
|
setSupportActionBar(settingsLayoutBinding.settingsToolbarLayout.toolbar);
|
||||||
|
|
||||||
if (savedInstanceBundle == null) {
|
if (restored) {
|
||||||
|
// Restore state
|
||||||
|
if (this.wasSearchActive) {
|
||||||
|
setSearchActive(true);
|
||||||
|
if (!TextUtils.isEmpty(this.searchText)) {
|
||||||
|
this.searchEditText.setText(this.searchText);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
getSupportFragmentManager().beginTransaction()
|
getSupportFragmentManager().beginTransaction()
|
||||||
.replace(R.id.settings_fragment_holder, new MainSettingsFragment())
|
.replace(R.id.settings_fragment_holder, new MainSettingsFragment())
|
||||||
.commit();
|
.commit();
|
||||||
@ -101,6 +122,12 @@ public class SettingsActivity extends AppCompatActivity implements
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onSaveInstanceState(@NonNull final Bundle outState) {
|
||||||
|
super.onSaveInstanceState(outState);
|
||||||
|
Icepick.saveInstanceState(this, outState);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onCreateOptionsMenu(final Menu menu) {
|
public boolean onCreateOptionsMenu(final Menu menu) {
|
||||||
final ActionBar actionBar = getSupportActionBar();
|
final ActionBar actionBar = getSupportActionBar();
|
||||||
@ -175,7 +202,10 @@ public class SettingsActivity extends AppCompatActivity implements
|
|||||||
//////////////////////////////////////////////////////////////////////////*/
|
//////////////////////////////////////////////////////////////////////////*/
|
||||||
//region Search
|
//region Search
|
||||||
|
|
||||||
private void initSearch(final SettingsLayoutBinding settingsLayoutBinding) {
|
private void initSearch(
|
||||||
|
final SettingsLayoutBinding settingsLayoutBinding,
|
||||||
|
final boolean restored
|
||||||
|
) {
|
||||||
searchContainer =
|
searchContainer =
|
||||||
settingsLayoutBinding.settingsToolbarLayout.toolbar
|
settingsLayoutBinding.settingsToolbarLayout.toolbar
|
||||||
.findViewById(R.id.toolbar_search_container);
|
.findViewById(R.id.toolbar_search_container);
|
||||||
@ -207,7 +237,19 @@ public class SettingsActivity extends AppCompatActivity implements
|
|||||||
.map(parser::parse)
|
.map(parser::parse)
|
||||||
.forEach(searcher::add);
|
.forEach(searcher::add);
|
||||||
|
|
||||||
searchFragment = new PreferenceSearchFragment(searcher);
|
if (restored) {
|
||||||
|
searchFragment = (PreferenceSearchFragment) getSupportFragmentManager()
|
||||||
|
.findFragmentByTag(PreferenceSearchFragment.NAME);
|
||||||
|
if (searchFragment != null) {
|
||||||
|
// Hide/Remove the search fragment otherwise we get an exception
|
||||||
|
// when adding it (because it's already present)
|
||||||
|
hideSearchFragment();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (searchFragment == null) {
|
||||||
|
searchFragment = new PreferenceSearchFragment();
|
||||||
|
}
|
||||||
|
searchFragment.setSearcher(searcher);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void prepareSearchConfig() {
|
private void prepareSearchConfig() {
|
||||||
@ -228,36 +270,45 @@ public class SettingsActivity extends AppCompatActivity implements
|
|||||||
|
|
||||||
public void setMenuSearchItem(final MenuItem menuSearchItem) {
|
public void setMenuSearchItem(final MenuItem menuSearchItem) {
|
||||||
this.menuSearchItem = menuSearchItem;
|
this.menuSearchItem = menuSearchItem;
|
||||||
|
|
||||||
|
// Ensure that the item is in the correct state when adding it. This is due to
|
||||||
|
// Android's lifecycle (the Activity is recreated before the Fragment that registers this)
|
||||||
|
if (menuSearchItem != null) {
|
||||||
|
menuSearchItem.setVisible(!isSearchActive());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setSearchActive(final boolean active) {
|
public void setSearchActive(final boolean active) {
|
||||||
|
if (DEBUG) {
|
||||||
|
Log.d(TAG, "setSearchActive called active=" + active);
|
||||||
|
}
|
||||||
|
|
||||||
// Ignore if search is already in correct state
|
// Ignore if search is already in correct state
|
||||||
if (isSearchActive() == active) {
|
if (isSearchActive() == active) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (DEBUG) {
|
wasSearchActive = active;
|
||||||
Log.d(TAG, "setSearchActive called active=" + active);
|
|
||||||
}
|
|
||||||
|
|
||||||
searchContainer.setVisibility(active ? View.VISIBLE : View.GONE);
|
searchContainer.setVisibility(active ? View.VISIBLE : View.GONE);
|
||||||
if (menuSearchItem != null) {
|
if (menuSearchItem != null) {
|
||||||
menuSearchItem.setVisible(!active);
|
menuSearchItem.setVisible(!active);
|
||||||
}
|
}
|
||||||
|
|
||||||
final FragmentManager fm = getSupportFragmentManager();
|
|
||||||
if (active) {
|
if (active) {
|
||||||
fm.beginTransaction()
|
getSupportFragmentManager()
|
||||||
|
.beginTransaction()
|
||||||
.add(FRAGMENT_HOLDER_ID, searchFragment, PreferenceSearchFragment.NAME)
|
.add(FRAGMENT_HOLDER_ID, searchFragment, PreferenceSearchFragment.NAME)
|
||||||
.addToBackStack(PreferenceSearchFragment.NAME)
|
.addToBackStack(PreferenceSearchFragment.NAME)
|
||||||
.commit();
|
.commit();
|
||||||
|
|
||||||
KeyboardUtil.showKeyboard(this, searchEditText);
|
KeyboardUtil.showKeyboard(this, searchEditText);
|
||||||
} else if (searchFragment != null) {
|
} else if (searchFragment != null) {
|
||||||
fm.beginTransaction().remove(searchFragment).commit();
|
hideSearchFragment();
|
||||||
fm.popBackStack(
|
getSupportFragmentManager()
|
||||||
PreferenceSearchFragment.NAME,
|
.popBackStack(
|
||||||
FragmentManager.POP_BACK_STACK_INCLUSIVE);
|
PreferenceSearchFragment.NAME,
|
||||||
|
FragmentManager.POP_BACK_STACK_INCLUSIVE);
|
||||||
|
|
||||||
KeyboardUtil.hideKeyboard(this, searchEditText);
|
KeyboardUtil.hideKeyboard(this, searchEditText);
|
||||||
}
|
}
|
||||||
@ -265,6 +316,10 @@ public class SettingsActivity extends AppCompatActivity implements
|
|||||||
resetSearchText();
|
resetSearchText();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void hideSearchFragment() {
|
||||||
|
getSupportFragmentManager().beginTransaction().remove(searchFragment).commit();
|
||||||
|
}
|
||||||
|
|
||||||
private void resetSearchText() {
|
private void resetSearchText() {
|
||||||
searchEditText.setText("");
|
searchEditText.setText("");
|
||||||
}
|
}
|
||||||
@ -279,7 +334,8 @@ public class SettingsActivity extends AppCompatActivity implements
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (searchFragment != null) {
|
if (searchFragment != null) {
|
||||||
searchFragment.updateSearchResults(this.searchEditText.getText().toString());
|
searchText = this.searchEditText.getText().toString();
|
||||||
|
searchFragment.updateSearchResults(searchText);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -24,14 +24,12 @@ import java.util.Objects;
|
|||||||
public class PreferenceSearchFragment extends Fragment {
|
public class PreferenceSearchFragment extends Fragment {
|
||||||
public static final String NAME = PreferenceSearchFragment.class.getSimpleName();
|
public static final String NAME = PreferenceSearchFragment.class.getSimpleName();
|
||||||
|
|
||||||
private final PreferenceSearcher searcher;
|
private PreferenceSearcher searcher;
|
||||||
|
|
||||||
private SearchViewHolder viewHolder;
|
private SearchViewHolder viewHolder;
|
||||||
private PreferenceSearchAdapter adapter;
|
private PreferenceSearchAdapter adapter;
|
||||||
|
|
||||||
public PreferenceSearchFragment(
|
public void setSearcher(final PreferenceSearcher searcher) {
|
||||||
final PreferenceSearcher searcher
|
|
||||||
) {
|
|
||||||
this.searcher = searcher;
|
this.searcher = searcher;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -56,7 +54,7 @@ public class PreferenceSearchFragment extends Fragment {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void updateSearchResults(final String keyword) {
|
public void updateSearchResults(final String keyword) {
|
||||||
if (adapter == null) {
|
if (adapter == null || searcher == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user