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

質問編集履歴

2

adMobが取り外せたので外しました

2019/03/29 02:51

投稿

akai_kinomi
akai_kinomi

スコア18

title CHANGED
File without changes
body CHANGED
@@ -6,8 +6,7 @@
6
6
  ・再度frag1に切り替えたとき、frag1_Worldが表示された状態にする。(画面保持)
7
7
 
8
8
 
9
- ```MainActivity.java
10
- import android.os.Build;
9
+ ```MainActivity.javaimport android.os.Build;
11
10
  import android.os.Bundle;
12
11
  import android.support.annotation.RequiresApi;
13
12
  import android.support.v4.app.FragmentManager;
@@ -16,9 +15,6 @@
16
15
  import android.util.Log;
17
16
  import android.widget.TabHost.TabSpec;
18
17
 
19
- import com.google.android.gms.ads.AdRequest;
20
- import com.google.android.gms.ads.AdView;
21
- import com.google.android.gms.ads.MobileAds;
22
18
 
23
19
  public class MainActivity extends AppCompatActivity {
24
20
 
@@ -59,21 +55,12 @@
59
55
  mTabHost.addTab(mTabSpec2, Fragment2.class, args);
60
56
  Log.d("debug","add");
61
57
 
62
-
63
- // Test App ID
64
- MobileAds.initialize(this,
65
- "ca-app-pub-3940256099942544~3347511713");
66
-
67
- AdView adView = findViewById(R.id.adView);
68
- AdRequest adRequest = new AdRequest.Builder().build();
69
- adView.loadAd(adRequest);
70
58
  }
71
59
  }
72
60
  }
73
61
 
74
62
  ```
75
- ```activity_main.xml
76
- <!-- タブ全体の定義 -->
63
+ ```activity_main.xml<!-- タブ全体の定義 -->
77
64
  <android.support.v4.app.FragmentTabHost xmlns:android="http://schemas.android.com/apk/res/android"
78
65
  android:id="@android:id/tabhost"
79
66
  xmlns:ads="http://schemas.android.com/apk/res-auto"
@@ -110,14 +97,6 @@
110
97
  android:layout_marginLeft="10dp"
111
98
  android:layout_marginRight="10dp"
112
99
  />
113
- <com.google.android.gms.ads.AdView
114
- android:id="@+id/adView"
115
- android:layout_width="wrap_content"
116
- android:layout_height="wrap_content"
117
- android:layout_gravity="center"
118
- ads:adSize="BANNER"
119
- ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
120
- </com.google.android.gms.ads.AdView>
121
100
  </LinearLayout>
122
101
  </android.support.v4.app.FragmentTabHost>
123
102
  ```

1

追記

2019/03/29 02:51

投稿

akai_kinomi
akai_kinomi

スコア18

title CHANGED
File without changes
body CHANGED
@@ -1,21 +1,12 @@
1
- ### 前提・実現したいこと
1
+ ### 実現したいこと
2
2
  Android Studioを使ってAndroidアプリを作成しています。
3
- Fragment使用
3
+ このプログラムで、下記動作実現たいです。
4
+ ・frag1でボタンを押す(textView:frag1_World)
4
- FragmentA
5
+ frag2に切り替える
5
- ・FragmentB
6
- の2つのページを切り替えられるアプリを作成し思います。
6
+ ・再度frag1に切り替えたとき、frag1_Worldが表示された状態に(画面保持)
7
7
 
8
8
 
9
- ### 発生している問題・エラーメッセージ
10
- FragmentAとFragmentBをタブで切り替えた際に、
11
- いままでの入力情報などが消えてしまうため、show/hideで切り替えたいです。
12
- どのように編集したら良いでしょうか。
13
-
14
-
15
9
  ```MainActivity.java
16
-
17
- import android.media.AudioAttributes;
18
- import android.media.SoundPool;
19
10
  import android.os.Build;
20
11
  import android.os.Bundle;
21
12
  import android.support.annotation.RequiresApi;
@@ -23,10 +14,8 @@
23
14
  import android.support.v4.app.FragmentTabHost;
24
15
  import android.support.v7.app.AppCompatActivity;
25
16
  import android.util.Log;
26
- import android.view.Menu;
27
17
  import android.widget.TabHost.TabSpec;
28
18
 
29
- import com.google.android.gms.ads.AdListener;
30
19
  import com.google.android.gms.ads.AdRequest;
31
20
  import com.google.android.gms.ads.AdView;
32
21
  import com.google.android.gms.ads.MobileAds;
@@ -34,36 +23,290 @@
34
23
  public class MainActivity extends AppCompatActivity {
35
24
 
36
25
  private FragmentTabHost mTabHost;
37
- private String[] TabTag = { "A", "B"};
26
+ private String[] TabTag = { "Dice", "Character"};
38
27
 
28
+ @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
39
29
  @Override
40
30
  protected void onCreate(Bundle savedInstanceState) {
41
31
  super.onCreate(savedInstanceState);
42
32
  setContentView(R.layout.activity_main);
43
33
 
44
- //FragmentManagerの取得
34
+ if (savedInstanceState == null) {
45
- FragmentManager mFragmentManager = getSupportFragmentManager();
46
35
 
47
- // FragmentTabHostを取得する
36
+ //FragmentManagerの取得
48
- mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
37
+ FragmentManager mFragmentManager = getSupportFragmentManager();
49
38
 
50
- // Context、FragmentManager、タブ選択時にFragment挿入するリソースID
39
+ // FragmentTabHost取得する
51
- mTabHost.setup(this, getSupportFragmentManager(), R.id.content);
40
+ mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
52
41
 
53
- //String型の引数には任意のidを渡す
54
- //今回は2つのFragmentをFragmentTabHostから切り替えるため、2つのTabSpecを用意する
42
+ // Context、FragmentManager、タブ選択時にFragmentを挿入するリソースID
55
- TabSpec mTabSpec1 = mTabHost.newTabSpec("tab1");
43
+ mTabHost.setup(this, getSupportFragmentManager(), R.id.content);
56
- TabSpec mTabSpec2 = mTabHost.newTabSpec("tab2");
57
44
 
58
- //Tab上表示する文字を渡す
45
+ //String型の引数は任意のidを渡す
46
+ //今回は2つのFragmentをFragmentTabHostから切り替えるため、2つのTabSpecを用意する
59
- mTabSpec1.setIndicator("Dice");
47
+ TabSpec mTabSpec1 = mTabHost.newTabSpec("tab1");
60
- mTabSpec2.setIndicator("Character");
48
+ TabSpec mTabSpec2 = mTabHost.newTabSpec("tab2");
61
49
 
62
- Bundle args = new Bundle();
50
+ //Tab上に表示する文字を渡す
63
- args.putString("string", "message");
51
+ mTabSpec1.setIndicator("frag1");
52
+ mTabSpec2.setIndicator("frag2");
64
53
 
54
+ Bundle args = new Bundle();
55
+ args.putString("string", "message");
56
+
57
+
65
- mTabHost.addTab(mTabSpec1, FragA.class, args);
58
+ mTabHost.addTab(mTabSpec1, Fragment1.class, args);
66
- mTabHost.addTab(mTabSpec2, FragB.class, args);
59
+ mTabHost.addTab(mTabSpec2, Fragment2.class, args);
67
-
60
+ Log.d("debug","add");
61
+
62
+
63
+ // Test App ID
64
+ MobileAds.initialize(this,
65
+ "ca-app-pub-3940256099942544~3347511713");
66
+
67
+ AdView adView = findViewById(R.id.adView);
68
+ AdRequest adRequest = new AdRequest.Builder().build();
69
+ adView.loadAd(adRequest);
70
+ }
68
71
  }
72
+ }
73
+
74
+ ```
75
+ ```activity_main.xml
76
+ <!-- タブ全体の定義 -->
77
+ <android.support.v4.app.FragmentTabHost xmlns:android="http://schemas.android.com/apk/res/android"
78
+ android:id="@android:id/tabhost"
79
+ xmlns:ads="http://schemas.android.com/apk/res-auto"
80
+ android:layout_width="match_parent"
81
+ android:layout_height="match_parent"
82
+ android:background="@color/background"
83
+ >
84
+ <!-- -->
85
+ <LinearLayout
86
+ android:layout_width="match_parent"
87
+ android:layout_height="match_parent"
88
+ android:orientation="vertical" >
89
+ <!-- タブパネル操作のためのTabWidgetを配置 -->
90
+ <TabWidget
91
+ android:id="@android:id/tabs"
92
+ android:layout_width="match_parent"
93
+ android:layout_height="wrap_content"
94
+ android:layout_weight="0"
95
+ android:orientation="horizontal" />
96
+ <!-- タブパネルを束ねるためのコンテナーを配置 -->
97
+ <FrameLayout
98
+ android:id="@android:id/tabcontent"
99
+ android:layout_width="0dp"
100
+ android:layout_height="0dp"
101
+ android:layout_weight="0" />
102
+ <!-- タブが押された時に表示するコンテンツ領域(Fragment) -->
103
+ <FrameLayout
104
+ android:id="@+id/content"
105
+ android:layout_width="match_parent"
106
+ android:layout_height="0dp"
107
+ android:layout_weight="1"
108
+ android:layout_marginTop="20dp"
109
+ android:layout_marginBottom="20dp"
110
+ android:layout_marginLeft="10dp"
111
+ android:layout_marginRight="10dp"
112
+ />
113
+ <com.google.android.gms.ads.AdView
114
+ android:id="@+id/adView"
115
+ android:layout_width="wrap_content"
116
+ android:layout_height="wrap_content"
117
+ android:layout_gravity="center"
118
+ ads:adSize="BANNER"
119
+ ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
120
+ </com.google.android.gms.ads.AdView>
121
+ </LinearLayout>
122
+ </android.support.v4.app.FragmentTabHost>
123
+ ```
124
+
125
+ ```Fragment1.java
126
+ import android.support.annotation.Nullable;
127
+ import android.support.v4.app.Fragment;
128
+ import android.os.Bundle;
129
+ import android.util.Log;
130
+ import android.view.LayoutInflater;
131
+
132
+
133
+ public class Fragment1 extends Fragment {
134
+
135
+ TextView textView1;
136
+ Button button1;
137
+
138
+ public void onCreate(Bundle savedInstanceState) {
139
+ super.onCreate(savedInstanceState);
140
+
141
+ // 初期設定 --------------------------------------------------------------------------------
142
+ //addTabの際にBundleを渡す場合は、Bundleから値を取得する
143
+ //渡さない(nullを渡している)場合は、実装しなくてよい。そうでないとgetString("string")時にエラーが発生する
144
+ Bundle args = getArguments();
145
+ String str = args.getString("string");
146
+ //-------------------------------------------------------------------------------
147
+ }
148
+
149
+ @Nullable
150
+ @Override
151
+ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
152
+ final View v = inflater.inflate(R.layout.frag1, null);
153
+
154
+
155
+ Log.d("debug", "frag1_OnCreateView");
156
+
157
+ // text ------------------------------------------------------------------------------------
158
+ textView1 = v.findViewById(R.id.text_view1);
159
+ textView1.setText("Frag1_Hello");
160
+
161
+ button1 = v.findViewById(R.id.button1);
162
+ button1.setOnClickListener(new View.OnClickListener() {
163
+ @Override
164
+ public void onClick(View v) {
165
+ textView1.setText("frag1_World");
166
+ }
167
+ });
168
+
169
+
170
+ return v;
171
+ }
172
+
173
+ @Override
174
+ public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
175
+ super.onViewCreated(view, savedInstanceState);
176
+ Log.d("test", "createFragment1");
177
+ }
178
+
179
+ @Override
180
+ public void onSaveInstanceState(Bundle outState) {
181
+ String i = "Hi";
182
+ outState.putString("editText", i);
183
+ super.onSaveInstanceState(outState);
184
+ }
185
+ @Override
186
+ public void onDestroyView() {
187
+ Log.d("test", "destroyFragment1");
188
+ super.onDestroyView();
189
+ }
190
+
191
+ }
192
+
193
+
194
+ ```
195
+
196
+ ```frag1.xml
197
+ <?xml version="1.0" encoding="utf-8"?>
198
+ <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
199
+ xmlns:tools="http://schemas.android.com/tools"
200
+ android:id="@+id/activity_main"
201
+ android:layout_width="match_parent"
202
+ android:layout_height="match_parent"
203
+ android:orientation="vertical"
204
+ tools:context=".MainActivity">
205
+
206
+ <TextView
207
+ android:id="@+id/text_view1"
208
+ android:layout_width="wrap_content"
209
+ android:layout_height="wrap_content"
210
+ android:textSize="30sp"
211
+ />
212
+
213
+ <Button
214
+ android:id="@+id/button1"
215
+ android:layout_width="wrap_content"
216
+ android:layout_height="wrap_content"
217
+ android:layout_margin="20dp"
218
+ android:text="tab1"
219
+ />
220
+
221
+ </LinearLayout>
222
+ ```
223
+ ```Fragment2.java
224
+ import android.support.annotation.Nullable;
225
+ import android.support.v4.app.Fragment;
226
+ import android.os.Bundle;
227
+ import android.util.Log;
228
+ import android.view.LayoutInflater;
229
+
230
+ /*
231
+ * タブがクリックされた時に表示するFragment
232
+ */
233
+ public class Fragment2 extends Fragment {
234
+
235
+ TextView textView2;
236
+ Button button2;
237
+
238
+
239
+ @Override
240
+ public void onCreate(Bundle savedInstanceState){
241
+ super.onCreate(savedInstanceState);
242
+
243
+ //addTabの際にBundleを渡す場合は、Bundleから値を取得する
244
+ //渡さない(nullを渡している)場合は、実装しなくてよい。そうでないとgetString("string")時にエラーが発生する
245
+ Bundle args = getArguments();
246
+ String str = args.getString("string");
247
+ }
248
+
249
+ @Nullable
250
+ @Override
251
+ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
252
+ View v = inflater.inflate(R.layout.frag2, null);
253
+
254
+ Log.d("debug", "frag2_OnCreateView");
255
+
256
+ // text ------------------------------------------------------------------------------------
257
+ textView2 = v.findViewById(R.id.text_view2);
258
+ textView2.setText("frag2_Hello");
259
+
260
+ button2 = v.findViewById(R.id.button2);
261
+ button2.setOnClickListener(new View.OnClickListener() {
262
+ @Override
263
+ public void onClick(View v) {
264
+ textView2.setText("frag2_World");
265
+ }
266
+ });
267
+ return v;
268
+ }
269
+ @Override
270
+ public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
271
+ super.onViewCreated(view, savedInstanceState);
272
+ Log.d("test", "createFragment2");
273
+ }
274
+
275
+ @Override
276
+ public void onSaveInstanceState(Bundle outState) {
277
+ String i = "Hi";
278
+ outState.putString("editText", i);
279
+ super.onSaveInstanceState(outState);
280
+ }
281
+ @Override
282
+ public void onDestroyView() {
283
+ Log.d("test", "destroyFragment2");
284
+ super.onDestroyView();
285
+ }
286
+ }
287
+ ```
288
+
289
+ ```frag2.xml
290
+ <?xml version="1.0" encoding="utf-8"?>
291
+ <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
292
+ android:orientation="vertical"
293
+ android:layout_width="match_parent"
294
+ android:layout_height="match_parent"
295
+ >
296
+ <TextView
297
+ android:id="@+id/text_view2"
298
+ android:layout_width="wrap_content"
299
+ android:layout_height="wrap_content"
300
+ android:textSize="30sp"
301
+ />
302
+
303
+ <Button
304
+ android:id="@+id/button2"
305
+ android:layout_width="wrap_content"
306
+ android:layout_height="wrap_content"
307
+ android:layout_margin="20dp"
308
+ android:text="tab2"
309
+ />
310
+
311
+ </LinearLayout>
69
312
  ```