|
16 | 16 |
|
17 | 17 | package com.google.firebase.quickstart.database.java;
|
18 | 18 |
|
19 |
| -import android.content.Intent; |
20 | 19 | import android.os.Bundle;
|
21 |
| -import android.view.Menu; |
22 |
| -import android.view.MenuItem; |
23 | 20 | import android.view.View;
|
24 | 21 |
|
25 |
| -import androidx.fragment.app.Fragment; |
26 |
| -import androidx.fragment.app.FragmentPagerAdapter; |
| 22 | +import androidx.annotation.NonNull; |
| 23 | +import androidx.annotation.Nullable; |
| 24 | +import androidx.appcompat.app.AppCompatActivity; |
| 25 | +import androidx.navigation.NavController; |
| 26 | +import androidx.navigation.NavDestination; |
| 27 | +import androidx.navigation.Navigation; |
27 | 28 |
|
28 |
| -import com.google.firebase.auth.FirebaseAuth; |
| 29 | +import com.google.android.material.floatingactionbutton.FloatingActionButton; |
29 | 30 | import com.google.firebase.quickstart.database.R;
|
30 | 31 | import com.google.firebase.quickstart.database.databinding.ActivityMainBinding;
|
31 |
| -import com.google.firebase.quickstart.database.java.fragment.MyPostsFragment; |
32 |
| -import com.google.firebase.quickstart.database.java.fragment.MyTopPostsFragment; |
33 |
| -import com.google.firebase.quickstart.database.java.fragment.RecentPostsFragment; |
34 | 32 |
|
35 |
| -public class MainActivity extends BaseActivity { |
| 33 | +public class MainActivity extends AppCompatActivity { |
36 | 34 |
|
37 |
| - private static final String TAG = "MainActivity"; |
| 35 | + private FloatingActionButton fab; |
| 36 | + private NavController navController; |
38 | 37 |
|
39 | 38 | @Override
|
40 | 39 | protected void onCreate(Bundle savedInstanceState) {
|
41 | 40 | super.onCreate(savedInstanceState);
|
42 | 41 | ActivityMainBinding binding = ActivityMainBinding.inflate(getLayoutInflater());
|
43 | 42 | setContentView(binding.getRoot());
|
| 43 | + setSupportActionBar(binding.toolbar); |
44 | 44 |
|
45 |
| - // Create the adapter that will return a fragment for each section |
46 |
| - FragmentPagerAdapter mPagerAdapter = new FragmentPagerAdapter(getSupportFragmentManager(), |
47 |
| - FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) { |
48 |
| - private final Fragment[] mFragments = new Fragment[]{ |
49 |
| - new RecentPostsFragment(), |
50 |
| - new MyPostsFragment(), |
51 |
| - new MyTopPostsFragment(), |
52 |
| - }; |
53 |
| - private final String[] mFragmentNames = new String[]{ |
54 |
| - getString(R.string.heading_recent), |
55 |
| - getString(R.string.heading_my_posts), |
56 |
| - getString(R.string.heading_my_top_posts) |
57 |
| - }; |
| 45 | + fab = binding.fab; |
58 | 46 |
|
| 47 | + navController = Navigation.findNavController(this, R.id.nav_host_fragment); |
| 48 | + navController.setGraph(R.navigation.nav_graph_java); |
| 49 | + navController.addOnDestinationChangedListener(new NavController.OnDestinationChangedListener() { |
59 | 50 | @Override
|
60 |
| - public Fragment getItem(int position) { |
61 |
| - return mFragments[position]; |
62 |
| - } |
63 |
| - |
64 |
| - @Override |
65 |
| - public int getCount() { |
66 |
| - return mFragments.length; |
67 |
| - } |
68 |
| - |
69 |
| - @Override |
70 |
| - public CharSequence getPageTitle(int position) { |
71 |
| - return mFragmentNames[position]; |
72 |
| - } |
73 |
| - }; |
74 |
| - // Set up the ViewPager with the sections adapter. |
75 |
| - binding.container.setAdapter(mPagerAdapter); |
76 |
| - binding.tabs.setupWithViewPager(binding.container); |
77 |
| - |
78 |
| - // Button launches NewPostActivity |
79 |
| - binding.fabNewPost.setOnClickListener(new View.OnClickListener() { |
80 |
| - @Override |
81 |
| - public void onClick(View v) { |
82 |
| - startActivity(new Intent(MainActivity.this, NewPostActivity.class)); |
| 51 | + public void onDestinationChanged(@NonNull NavController controller, @NonNull NavDestination destination, @Nullable Bundle arguments) { |
| 52 | + if (destination.getId() == R.id.MainFragment) { |
| 53 | + fab.setVisibility(View.VISIBLE); |
| 54 | + fab.setOnClickListener(new View.OnClickListener() { |
| 55 | + @Override |
| 56 | + public void onClick(View view) { |
| 57 | + navController.navigate(R.id.action_MainFragment_to_NewPostFragment); |
| 58 | + } |
| 59 | + }); |
| 60 | + } else { |
| 61 | + fab.setVisibility(View.GONE); |
| 62 | + } |
83 | 63 | }
|
84 | 64 | });
|
85 | 65 | }
|
86 |
| - |
87 |
| - @Override |
88 |
| - public boolean onCreateOptionsMenu(Menu menu) { |
89 |
| - getMenuInflater().inflate(R.menu.menu_main, menu); |
90 |
| - return true; |
91 |
| - } |
92 |
| - |
93 |
| - @Override |
94 |
| - public boolean onOptionsItemSelected(MenuItem item) { |
95 |
| - int i = item.getItemId(); |
96 |
| - if (i == R.id.action_logout) { |
97 |
| - FirebaseAuth.getInstance().signOut(); |
98 |
| - startActivity(new Intent(this, SignInActivity.class)); |
99 |
| - finish(); |
100 |
| - return true; |
101 |
| - } else { |
102 |
| - return super.onOptionsItemSelected(item); |
103 |
| - } |
104 |
| - } |
105 |
| - |
106 | 66 | }
|
0 commit comments