Search in sources :

Example 1 with NavOptions

use of androidx.navigation.NavOptions in project randomix by m-i-n-a-r.

the class MainActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    NavController navController;
    // Retrieve the shared preferences
    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
    String theme = sp.getString("theme_color", "system");
    String accent = sp.getString("accent_color", "system");
    String lastItem = sp.getString("last_page", "roulette");
    if (!sp.getBoolean("first", false)) {
        SharedPreferences.Editor editor = sp.edit();
        editor.putBoolean("first", true);
        editor.apply();
        // Call the AppIntro activity
        Intent intent = new Intent(this, IntroActivity.class);
        startActivity(intent);
        finish();
    }
    // Set the base theme and the accent
    assert theme != null;
    if (theme.equals("system"))
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
    if (theme.equals("dark"))
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
    if (theme.equals("light"))
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
    assert accent != null;
    // Default
    if (accent.equals("system"))
        setTheme(R.style.AppTheme_System);
    if (accent.equals("blue"))
        setTheme(R.style.AppTheme_Blue);
    if (accent.equals("green"))
        setTheme(R.style.AppTheme_Green);
    if (accent.equals("aqua"))
        setTheme(R.style.AppTheme_Aqua);
    if (accent.equals("orange"))
        setTheme(R.style.AppTheme_Orange);
    if (accent.equals("yellow"))
        setTheme(R.style.AppTheme_Yellow);
    if (accent.equals("teal"))
        setTheme(R.style.AppTheme_Teal);
    if (accent.equals("violet"))
        setTheme(R.style.AppTheme_Violet);
    if (accent.equals("pink"))
        setTheme(R.style.AppTheme_Pink);
    if (accent.equals("lightBlue"))
        setTheme(R.style.AppTheme_LightBlue);
    if (accent.equals("red"))
        setTheme(R.style.AppTheme_Red);
    if (accent.equals("lime"))
        setTheme(R.style.AppTheme_Lime);
    if (accent.equals("crimson"))
        setTheme(R.style.AppTheme_Crimson);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // Get the bottom navigation bar and configure it for the navigation plugin
    BottomNavigationView navigation = findViewById(R.id.navigation);
    NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.navHostFragment);
    navController = Objects.requireNonNull(navHostFragment).getNavController();
    NavOptions options = new NavOptions.Builder().setLaunchSingleTop(true).setEnterAnim(R.anim.nav_enter_anim).setExitAnim(R.anim.nav_exit_anim).setPopEnterAnim(R.anim.nav_pop_enter_anim).setPopExitAnim(R.anim.nav_pop_exit_anim).setPopUpTo(R.id.nav_graph, true).build();
    // Only way to get the animations back at the moment
    navigation.setOnItemSelectedListener(item -> {
        if (item.getItemId() == R.id.navigationRoulette) {
            // Default starting page
            sp.edit().putString("last_page", "roulette").apply();
            navController.navigate(R.id.navigationRoulette, null, options);
        }
        if (item.getItemId() == R.id.navigationCoin) {
            sp.edit().putString("last_page", "coin").apply();
            navController.navigate(R.id.navigationCoin, null, options);
        }
        if (item.getItemId() == R.id.navigationMagicBall) {
            sp.edit().putString("last_page", "magicBall").apply();
            navController.navigate(R.id.navigationMagicBall, null, options);
        }
        if (item.getItemId() == R.id.navigationDice) {
            sp.edit().putString("last_page", "dice").apply();
            navController.navigate(R.id.navigationDice, null, options);
        }
        if (item.getItemId() == R.id.navigationSettings) {
            sp.edit().putString("last_page", "settings").apply();
            navController.navigate(R.id.navigationSettings, null, options);
        }
        return true;
    });
    navigation.setOnItemReselectedListener(item -> {
    // Just do nothing when an item is reselected from the bottom navigation bar
    });
    // Reopen the last selected tab
    switch(lastItem) {
        case "coin":
            navigation.setSelectedItemId(R.id.navigationCoin);
            break;
        case "dice":
            navigation.setSelectedItemId(R.id.navigationDice);
            break;
        case "magicBall":
            navigation.setSelectedItemId(R.id.navigationMagicBall);
            break;
        case "settings":
            navigation.setSelectedItemId(R.id.navigationSettings);
            break;
    }
    // Rating stuff
    AppRater.appLaunched(this);
}
Also used : NavOptions(androidx.navigation.NavOptions) SharedPreferences(android.content.SharedPreferences) BottomNavigationView(com.google.android.material.bottomnavigation.BottomNavigationView) NavController(androidx.navigation.NavController) Intent(android.content.Intent) NavHostFragment(androidx.navigation.fragment.NavHostFragment)

Example 2 with NavOptions

use of androidx.navigation.NavOptions in project spacecraft-android by JamesfChen.

the class HomeFragment method onViewCreated.

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    final NavOptions options = new NavOptions.Builder().build();
    final NavController navController = new NavController(getActivity());
    // navController.navigate(R.id.action_step_two);
    // navController.navigate(R.id.fragment_flow_step_one_dest);
    // navController.navigate(R.id.action_step_two, null, options);
    navController.addOnDestinationChangedListener((controller, destination, arguments) -> {
        NavDestination currentDestination = navController.getCurrentDestination();
        NavGraph graph = navController.getGraph();
    // navController.navigateUp()
    });
    Navigation.setViewNavController(view, navController);
    view.findViewById(R.id.bt_navigate_destination).setOnClickListener(view1 -> Navigation.findNavController(view1).navigate(R.id.fragment_flow_step_one_dest, null, options));
    // view.findViewById(R.id.bt_navigate_action).setOnClickListener(
    // Navigation.createNavigateOnClickListener(R.id.action_step_two)
    // );
    AppBarLayout abl = view.findViewById(R.id.abl);
    abl.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {

        @Override
        public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
        }
    });
    CollapsingToolbarLayout ctbl = view.findViewById(R.id.ctbl);
    rv = view.findViewById(R.id.rv);
    rv.setAdapter(new MyAdapter());
    rv.setLayoutManager(new LinearLayoutManager(rv.getContext(), RecyclerView.VERTICAL, false));
    CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) rv.getLayoutParams();
    AppBarLayout.ScrollingViewBehavior behavior = (AppBarLayout.ScrollingViewBehavior) layoutParams.getBehavior();
    behavior.setOverlayTop(120);
    lv = view.findViewById(R.id.lv);
    lv.setNestedScrollingEnabled(true);
    lv.setAdapter(new BaseAdapter() {

        @Override
        public int getCount() {
            return 23;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Log.d("ParallaxActivity", "ListView:getView:position" + position);
            MyViewHolder myViewHolder = null;
            if (convertView == null) {
                View itemView = LayoutInflater.from(getActivity()).inflate(R.layout.item_image_and_text, parent, false);
                myViewHolder = new MyViewHolder(itemView);
                itemView.setTag(myViewHolder);
                convertView = itemView;
            } else {
                myViewHolder = (MyViewHolder) convertView.getTag();
            }
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            myViewHolder.tvText.setText("ListView:position:" + position);
            return convertView;
        }
    });
}
Also used : NavDestination(androidx.navigation.NavDestination) NavOptions(androidx.navigation.NavOptions) ViewGroup(android.view.ViewGroup) NavController(androidx.navigation.NavController) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) TextView(android.widget.TextView) View(android.view.View) ListView(android.widget.ListView) RecyclerView(androidx.recyclerview.widget.RecyclerView) CoordinatorLayout(androidx.coordinatorlayout.widget.CoordinatorLayout) CollapsingToolbarLayout(com.google.android.material.appbar.CollapsingToolbarLayout) NavGraph(androidx.navigation.NavGraph) AppBarLayout(com.google.android.material.appbar.AppBarLayout) BaseAdapter(android.widget.BaseAdapter)

Aggregations

NavController (androidx.navigation.NavController)2 NavOptions (androidx.navigation.NavOptions)2 Intent (android.content.Intent)1 SharedPreferences (android.content.SharedPreferences)1 View (android.view.View)1 ViewGroup (android.view.ViewGroup)1 BaseAdapter (android.widget.BaseAdapter)1 ListView (android.widget.ListView)1 TextView (android.widget.TextView)1 CoordinatorLayout (androidx.coordinatorlayout.widget.CoordinatorLayout)1 NavDestination (androidx.navigation.NavDestination)1 NavGraph (androidx.navigation.NavGraph)1 NavHostFragment (androidx.navigation.fragment.NavHostFragment)1 LinearLayoutManager (androidx.recyclerview.widget.LinearLayoutManager)1 RecyclerView (androidx.recyclerview.widget.RecyclerView)1 AppBarLayout (com.google.android.material.appbar.AppBarLayout)1 CollapsingToolbarLayout (com.google.android.material.appbar.CollapsingToolbarLayout)1 BottomNavigationView (com.google.android.material.bottomnavigation.BottomNavigationView)1