Improve navigation/backstack
This commit is contained in:
parent
6fb1847327
commit
31e5a7afb0
3 changed files with 18 additions and 18 deletions
|
@ -25,7 +25,6 @@ import android.content.SharedPreferences;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.preference.PreferenceManager;
|
import android.preference.PreferenceManager;
|
||||||
import android.support.v4.app.Fragment;
|
import android.support.v4.app.Fragment;
|
||||||
import android.support.v4.app.FragmentManager;
|
|
||||||
import android.support.v7.app.ActionBar;
|
import android.support.v7.app.ActionBar;
|
||||||
import android.support.v7.app.AppCompatActivity;
|
import android.support.v7.app.AppCompatActivity;
|
||||||
import android.support.v7.widget.Toolbar;
|
import android.support.v7.widget.Toolbar;
|
||||||
|
@ -37,7 +36,6 @@ import android.view.View;
|
||||||
|
|
||||||
import org.schabi.newpipe.download.DownloadActivity;
|
import org.schabi.newpipe.download.DownloadActivity;
|
||||||
import org.schabi.newpipe.extractor.StreamingService;
|
import org.schabi.newpipe.extractor.StreamingService;
|
||||||
import org.schabi.newpipe.fragments.MainFragment;
|
|
||||||
import org.schabi.newpipe.fragments.detail.VideoDetailFragment;
|
import org.schabi.newpipe.fragments.detail.VideoDetailFragment;
|
||||||
import org.schabi.newpipe.fragments.search.SearchFragment;
|
import org.schabi.newpipe.fragments.search.SearchFragment;
|
||||||
import org.schabi.newpipe.settings.SettingsActivity;
|
import org.schabi.newpipe.settings.SettingsActivity;
|
||||||
|
@ -104,12 +102,10 @@ public class MainActivity extends AppCompatActivity {
|
||||||
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.fragment_holder);
|
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.fragment_holder);
|
||||||
if (fragment instanceof VideoDetailFragment) if (((VideoDetailFragment) fragment).onActivityBackPressed()) return;
|
if (fragment instanceof VideoDetailFragment) if (((VideoDetailFragment) fragment).onActivityBackPressed()) return;
|
||||||
|
|
||||||
super.onBackPressed();
|
|
||||||
|
|
||||||
fragment = getSupportFragmentManager().findFragmentById(R.id.fragment_holder);
|
if (getSupportFragmentManager().getBackStackEntryCount() == 1) {
|
||||||
if (getSupportFragmentManager().getBackStackEntryCount() == 0 && !(fragment instanceof MainFragment)) {
|
finish();
|
||||||
super.onBackPressed();
|
} else super.onBackPressed();
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*//////////////////////////////////////////////////////////////////////////
|
/*//////////////////////////////////////////////////////////////////////////
|
||||||
|
@ -148,11 +144,7 @@ public class MainActivity extends AppCompatActivity {
|
||||||
|
|
||||||
switch (id) {
|
switch (id) {
|
||||||
case android.R.id.home: {
|
case android.R.id.home: {
|
||||||
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.fragment_holder);
|
NavigationHelper.gotoMainFragment(getSupportFragmentManager());
|
||||||
if (fragment instanceof VideoDetailFragment) ((VideoDetailFragment) fragment).clearHistory();
|
|
||||||
|
|
||||||
getSupportFragmentManager().popBackStackImmediate(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
|
|
||||||
NavigationHelper.openMainFragment(getSupportFragmentManager());
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
case R.id.action_settings: {
|
case R.id.action_settings: {
|
||||||
|
@ -178,9 +170,9 @@ public class MainActivity extends AppCompatActivity {
|
||||||
//////////////////////////////////////////////////////////////////////////*/
|
//////////////////////////////////////////////////////////////////////////*/
|
||||||
|
|
||||||
private void initFragments() {
|
private void initFragments() {
|
||||||
if (getIntent() != null && getIntent().hasExtra(Constants.KEY_URL)) {
|
if (getIntent() != null && getIntent().hasExtra(Constants.KEY_LINK_TYPE)) {
|
||||||
handleIntent(getIntent());
|
handleIntent(getIntent());
|
||||||
} else NavigationHelper.openMainFragment(getSupportFragmentManager());
|
} else NavigationHelper.gotoMainFragment(getSupportFragmentManager());
|
||||||
}
|
}
|
||||||
|
|
||||||
/*//////////////////////////////////////////////////////////////////////////
|
/*//////////////////////////////////////////////////////////////////////////
|
||||||
|
@ -209,8 +201,7 @@ public class MainActivity extends AppCompatActivity {
|
||||||
int serviceId = intent.getIntExtra(Constants.KEY_SERVICE_ID, 0);
|
int serviceId = intent.getIntExtra(Constants.KEY_SERVICE_ID, 0);
|
||||||
NavigationHelper.openSearchFragment(getSupportFragmentManager(), serviceId, searchQuery);
|
NavigationHelper.openSearchFragment(getSupportFragmentManager(), serviceId, searchQuery);
|
||||||
} else {
|
} else {
|
||||||
getSupportFragmentManager().popBackStackImmediate(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
|
NavigationHelper.gotoMainFragment(getSupportFragmentManager());
|
||||||
NavigationHelper.openMainFragment(getSupportFragmentManager());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -380,7 +380,7 @@ public class SearchFragment extends BaseFragment implements SuggestionWorker.OnS
|
||||||
public void onClick(View v) {
|
public void onClick(View v) {
|
||||||
if (DEBUG) Log.d(TAG, "onClick() called with: v = [" + v + "]");
|
if (DEBUG) Log.d(TAG, "onClick() called with: v = [" + v + "]");
|
||||||
if (TextUtils.isEmpty(searchEditText.getText())) {
|
if (TextUtils.isEmpty(searchEditText.getText())) {
|
||||||
NavigationHelper.openMainFragment(getFragmentManager());
|
NavigationHelper.gotoMainFragment(getFragmentManager());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -24,6 +24,7 @@ import org.schabi.newpipe.player.VideoPlayer;
|
||||||
|
|
||||||
@SuppressWarnings({"unused", "WeakerAccess"})
|
@SuppressWarnings({"unused", "WeakerAccess"})
|
||||||
public class NavigationHelper {
|
public class NavigationHelper {
|
||||||
|
public static final String MAIN_FRAGMENT_TAG = "main_fragment_tag";
|
||||||
|
|
||||||
/*//////////////////////////////////////////////////////////////////////////
|
/*//////////////////////////////////////////////////////////////////////////
|
||||||
// Players
|
// Players
|
||||||
|
@ -75,11 +76,19 @@ public class NavigationHelper {
|
||||||
// Through FragmentManager
|
// Through FragmentManager
|
||||||
//////////////////////////////////////////////////////////////////////////*/
|
//////////////////////////////////////////////////////////////////////////*/
|
||||||
|
|
||||||
public static void openMainFragment(FragmentManager fragmentManager) {
|
public static void gotoMainFragment(FragmentManager fragmentManager) {
|
||||||
ImageLoader.getInstance().clearMemoryCache();
|
ImageLoader.getInstance().clearMemoryCache();
|
||||||
|
|
||||||
|
boolean popped = fragmentManager.popBackStackImmediate(MAIN_FRAGMENT_TAG, 0);
|
||||||
|
if (!popped) openMainFragment(fragmentManager);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void openMainFragment(FragmentManager fragmentManager) {
|
||||||
|
fragmentManager.popBackStackImmediate(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
|
||||||
fragmentManager.beginTransaction()
|
fragmentManager.beginTransaction()
|
||||||
.setCustomAnimations(R.anim.custom_fade_in, R.anim.custom_fade_out, R.anim.custom_fade_in, R.anim.custom_fade_out)
|
.setCustomAnimations(R.anim.custom_fade_in, R.anim.custom_fade_out, R.anim.custom_fade_in, R.anim.custom_fade_out)
|
||||||
.replace(R.id.fragment_holder, new MainFragment())
|
.replace(R.id.fragment_holder, new MainFragment())
|
||||||
|
.addToBackStack(MAIN_FRAGMENT_TAG)
|
||||||
.commit();
|
.commit();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue