teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

コード追加

2020/02/20 01:57

投稿

luckyclock
luckyclock

スコア74

title CHANGED
File without changes
body CHANGED
@@ -10,4 +10,139 @@
10
10
  補足
11
11
  動きを見てみましたが
12
12
  2回目に同じアイテムをタップした場合、新たに生成しようとonCreateが走ったあとすぐに最初に動いていたものを終了しようとonDestroyが走ってしまっています。
13
- これは単純に上記の方法で解決できるのだろうか・・・
13
+ これは単純に上記の方法で解決できるのだろうか・・・
14
+ 1.案で解決できそうですが、表示しているフラグメントと同じアイテム選択時は何も処理しないとかできますか?
15
+ 静的なfragmentなのでもしかして大幅につくりかえないと駄目でしょうか?
16
+
17
+
18
+ MainActivity.java
19
+ ```java
20
+ import android.Manifest;
21
+ import android.content.Intent;
22
+ import android.content.pm.PackageManager;
23
+ import android.os.Bundle;
24
+
25
+ import com.google.android.material.floatingactionbutton.FloatingActionButton;
26
+ import com.google.android.material.snackbar.Snackbar;
27
+
28
+ import android.view.MenuItem;
29
+ import android.view.View;
30
+
31
+ import androidx.annotation.NonNull;
32
+ import androidx.core.app.ActivityCompat;
33
+ import androidx.core.content.ContextCompat;
34
+ import androidx.navigation.NavController;
35
+ import androidx.navigation.Navigation;
36
+ import androidx.navigation.ui.AppBarConfiguration;
37
+ import androidx.navigation.ui.NavigationUI;
38
+
39
+ import com.google.android.material.navigation.NavigationView;
40
+
41
+ import androidx.drawerlayout.widget.DrawerLayout;
42
+
43
+ import androidx.appcompat.app.AppCompatActivity;
44
+ import androidx.appcompat.widget.Toolbar;
45
+
46
+ import android.view.Menu;
47
+
48
+ public class MainActivity extends AppCompatActivity {
49
+
50
+ private AppBarConfiguration mAppBarConfiguration;
51
+
52
+ @Override
53
+ protected void onCreate(Bundle savedInstanceState) {
54
+ super.onCreate(savedInstanceState);
55
+ setContentView(R.layout.activity_main);
56
+ Toolbar toolbar = findViewById(R.id.toolbar);
57
+ setSupportActionBar(toolbar);
58
+ FloatingActionButton fab = findViewById(R.id.fab);
59
+ fab.setOnClickListener(new View.OnClickListener() {
60
+ @Override
61
+ public void onClick(View view) {
62
+ Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
63
+ .setAction("Action", null).show();
64
+ }
65
+ });
66
+ DrawerLayout drawer = findViewById(R.id.drawer_layout);
67
+ NavigationView navigationView = findViewById(R.id.nav_view);
68
+
69
+ // Passing each menu ID as a set of Ids because each
70
+ // menu should be considered as top level destinations.
71
+ mAppBarConfiguration = new AppBarConfiguration.Builder(
72
+ R.id.nav_home, R.id.nav_graph, R.id.nav_slideshow,
73
+ R.id.nav_tools, R.id.nav_share, R.id.nav_send)
74
+ .setDrawerLayout(drawer)
75
+ .build();
76
+ NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
77
+ NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
78
+ NavigationUI.setupWithNavController(navigationView, navController);
79
+ }
80
+
81
+ @Override
82
+ public boolean onCreateOptionsMenu(Menu menu) {
83
+ // Inflate the menu; this adds items to the action bar if it is present.
84
+ getMenuInflater().inflate(R.menu.main, menu);
85
+ return true;
86
+ }
87
+
88
+ @Override
89
+ public boolean onSupportNavigateUp() {
90
+ NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
91
+ return NavigationUI.navigateUp(navController, mAppBarConfiguration)
92
+ || super.onSupportNavigateUp();
93
+ }
94
+
95
+ @Override
96
+ protected void onDestroy()
97
+ {
98
+ super.onDestroy();
99
+ }
100
+ }
101
+ ```
102
+
103
+ mobile_navigation.xml
104
+ ```XML
105
+ <?xml version="1.0" encoding="utf-8"?>
106
+ <navigation xmlns:android="http://schemas.android.com/apk/res/android"
107
+ xmlns:app="http://schemas.android.com/apk/res-auto"
108
+ xmlns:tools="http://schemas.android.com/tools"
109
+ android:id="@+id/mobile_navigation"
110
+ app:startDestination="@+id/nav_home">
111
+
112
+ <fragment
113
+ android:id="@+id/nav_home"
114
+ android:name="XXX.ui.home.HomeFragment"
115
+ android:label="@string/menu_home"
116
+ tools:layout="@layout/fragment_home" />
117
+
118
+ <fragment
119
+ android:id="@+id/nav_graph"
120
+ android:name="XXX.ui.graph.GraphFragment"
121
+ android:label="@string/menu_graph"
122
+ tools:layout="@layout/fragment_graph" />
123
+
124
+ <fragment
125
+ android:id="@+id/nav_slideshow"
126
+ android:name="xxx.ui.slideshow.SlideshowFragment"
127
+ android:label="@string/menu_slideshow"
128
+ tools:layout="@layout/fragment_slideshow" />
129
+
130
+ <fragment
131
+ android:id="@+id/nav_tools"
132
+ android:name="xxx.ui.tools.ToolsFragment"
133
+ android:label="@string/menu_tools"
134
+ tools:layout="@layout/fragment_tools" />
135
+
136
+ <fragment
137
+ android:id="@+id/nav_share"
138
+ android:name="xxx.ui.share.ShareFragment"
139
+ android:label="@string/menu_share"
140
+ tools:layout="@layout/fragment_share" />
141
+
142
+ <fragment
143
+ android:id="@+id/nav_send"
144
+ android:name="xxx.ui.send.SendFragment"
145
+ android:label="@string/menu_send"
146
+ tools:layout="@layout/fragment_send" />
147
+ </navigation>
148
+ ```

1

補足追加

2020/02/20 01:57

投稿

luckyclock
luckyclock

スコア74

title CHANGED
File without changes
body CHANGED
@@ -5,4 +5,9 @@
5
5
  2.一度fragmentのインスタンスが生成されていたら再生成しない
6
6
  上記二つ思いついたのですが、解決方法として正しいのかどうか。
7
7
  そして実装上どのようにすれば実現できるか知りたいです。
8
- 1.の解決方法はデフォルトのテンプレートで各アイテムがfragmentタグでxmlに埋め込まれいるので難しいような気がしています。
8
+ 1.の解決方法はデフォルトのテンプレートで各アイテムがfragmentタグでxmlに埋め込まれいるので難しいような気がしています。
9
+
10
+ 補足
11
+ 動きを見てみましたが
12
+ 2回目に同じアイテムをタップした場合、新たに生成しようとonCreateが走ったあとすぐに最初に動いていたものを終了しようとonDestroyが走ってしまっています。
13
+ これは単純に上記の方法で解決できるのだろうか・・・