Skip to content

Commit 1318f5c

Browse files
author
柳澤 雄高
committed
Parcelableの実装確認用
1 parent d8c3197 commit 1318f5c

File tree

4 files changed

+117
-3
lines changed

4 files changed

+117
-3
lines changed

StudyAplication/app/src/main/java/app/maruty/com/studyaplication/MainActivity.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,26 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
1919
protected void onCreate(Bundle savedInstanceState) {
2020
super.onCreate(savedInstanceState);
2121
setContentView(R.layout.activity_main);
22+
23+
//ライフサイクル確認用
2224
Toast.makeText(this.getApplicationContext(),"onCreate", Toast.LENGTH_SHORT).show();
25+
26+
//各種ボタンのオブジェクト
2327
Button button1 = (Button)findViewById(R.id.button01);
2428
Button button2 = (Button)findViewById(R.id.button02);
29+
Button button3 = (Button)findViewById(R.id.button03);
30+
31+
//各種ボタンのリスナー
2532
button1.setOnClickListener(this);
2633
button2.setOnClickListener(this);
34+
button3.setOnClickListener(this);
2735

2836
}
2937

3038
@Override
3139
protected void onStart() {
3240
super.onStart();
41+
//ライフサイクル確認用
3342
Toast.makeText(this.getApplicationContext(),"onStart", Toast.LENGTH_SHORT).show();
3443

3544
}
@@ -38,48 +47,63 @@ protected void onStart() {
3847
@Override
3948
protected void onResume(){
4049
super.onResume();
50+
//ライフサイクル確認用
4151
Toast.makeText(this.getApplicationContext(),"onResume", Toast.LENGTH_SHORT).show();
4252

4353
}
4454

4555
@Override
4656
protected void onPause(){
4757
super.onPause();
58+
//ライフサイクル確認用
4859
Toast.makeText(this.getApplicationContext(),"onPause", Toast.LENGTH_SHORT).show();
4960

5061
}
5162

5263
@Override
5364
protected void onStop() {
5465
super.onStop();
66+
//ライフサイクル確認用
5567
Toast.makeText(this.getApplicationContext(),"onStop", Toast.LENGTH_SHORT).show();
5668

5769
}
5870

5971
@Override
6072
protected void onDestroy() {
6173
super.onDestroy();
74+
//ライフサイクル確認用
6275
Toast.makeText(this.getApplicationContext(),"onDestroy", Toast.LENGTH_SHORT).show();
6376

6477
}
6578

6679
@Override
6780
protected void onRestart() {
6881
super.onRestart();
82+
//ライフサイクル確認用
6983
Toast.makeText(this.getApplicationContext(),"onRestart", Toast.LENGTH_SHORT).show();
7084
}
7185

7286
public void onClick(View v){
7387
switch (v.getId()) {
7488
case R.id.button01:
89+
//明示的インテントサンプル
7590
Intent intent1 = new Intent(this , SubActivity.class);
7691
intent1.putExtra("data","100");
7792
startActivity(intent1);
7893
break;
7994
case R.id.button02:
95+
//暗黙的インテントサンプル
8096
Uri uri = Uri.parse("http://google.com");
8197
Intent intent2 = new Intent(Intent.ACTION_VIEW,uri);
8298
startActivity(intent2);
99+
break;
100+
case R.id.button03:
101+
//Parcelableサンプル
102+
SampleParcelable sampleParcelable = new SampleParcelable();
103+
Intent intent3 = new Intent(this, SubActivity.class);
104+
intent3.putExtra("parcelable",sampleParcelable);
105+
startActivity(intent3);
106+
break;
83107

84108
default:
85109
break;
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package app.maruty.com.studyaplication;
2+
3+
4+
import android.os.Parcel;
5+
import android.os.Parcelable;
6+
7+
public class SampleParcelable implements Parcelable{
8+
9+
10+
public String sampleParcelableVariable = "unko";
11+
12+
13+
//コンストラクタ
14+
protected SampleParcelable() {
15+
16+
}
17+
18+
//コンストラクタ
19+
protected SampleParcelable(Parcel in) {
20+
sampleParcelableVariable = in.readString();
21+
}
22+
23+
//Interfaceの実装
24+
@Override
25+
public int describeContents() {
26+
return 0;
27+
}
28+
29+
//Interfaceの実装
30+
@Override
31+
public void writeToParcel(Parcel dest, int flags) {
32+
dest.writeString(sampleParcelableVariable);
33+
}
34+
35+
//Interfaceの実装
36+
public static final Creator<SampleParcelable> CREATOR = new Creator<SampleParcelable>() {
37+
@Override
38+
public SampleParcelable createFromParcel(Parcel in) {
39+
return new SampleParcelable(in);
40+
}
41+
42+
@Override
43+
public SampleParcelable[] newArray(int size) {
44+
return new SampleParcelable[size];
45+
}
46+
};
47+
}
Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,50 @@
11
package app.maruty.com.studyaplication;
22

3+
import android.annotation.SuppressLint;
34
import android.content.Intent;
45
import android.os.Bundle;
56
import android.support.v7.app.AppCompatActivity;
67
import android.widget.TextView;
78

89
public class SubActivity extends AppCompatActivity {
910

11+
1012
@Override
1113
protected void onCreate(Bundle savedInstanceState) {
1214
super.onCreate(savedInstanceState);
1315
setContentView(R.layout.activity_main);
14-
Intent intent = getIntent();
15-
String number = intent.getStringExtra("data");
1616

17+
18+
/*
19+
Bundle or Intent
20+
Intentの内部にBundleを保持
21+
*/
22+
23+
24+
String number ="";
25+
SampleParcelable sampleParcelable = new SampleParcelable();
1726
TextView textView = (TextView) findViewById(R.id.text);
1827

19-
textView.setText("SubActivity" + number);
28+
textView.setText("SubActivity");
29+
//IntentでもらうパターンBundleから取り出すやり方
30+
Intent intent = getIntent();
31+
Bundle extras = getIntent().getExtras();
32+
33+
if(extras.containsKey("data")){
34+
35+
//Intentから
36+
number = intent.getStringExtra("data");
37+
textView.setText(textView.getText() + number);
38+
39+
}
40+
if(extras.containsKey("parcelable")){
41+
//Bundleから
42+
sampleParcelable = (SampleParcelable) extras.getParcelable("parcelable");
43+
44+
TextView textView01 = (TextView)findViewById(R.id.text01);
45+
textView01.setText(sampleParcelable.sampleParcelableVariable);
46+
47+
}
48+
2049
}
2150
}

StudyAplication/app/src/main/res/layout/activity_main.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@
2222
android:text="MainActivity"
2323
tools:ignore="HardcodedText"/>
2424

25+
<TextView
26+
android:id="@+id/text01"
27+
android:layout_width="wrap_content"
28+
android:layout_height="wrap_content"
29+
android:text="parcelable表示確認"
30+
tools:ignore="HardcodedText"/>
31+
2532
<Button
2633
android:id="@+id/button01"
2734
android:layout_width="wrap_content"
@@ -35,5 +42,12 @@
3542
android:layout_height="wrap_content"
3643
android:text="暗黙的intent"
3744
tools:ignore="HardcodedText"/>
45+
46+
<Button
47+
android:id="@+id/button03"
48+
android:layout_width="wrap_content"
49+
android:layout_height="wrap_content"
50+
android:text="parcelable確認"
51+
tools:ignore="HardcodedText"/>
3852
</LinearLayout>
3953
</LinearLayout>

0 commit comments

Comments
 (0)