ASスレッド編①解説~別アクティビティの別スレッドで作成した配列をうけつとる~

MainActivity.java

package com.example.roomhandler;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {



static String[] converts;  ・・・➀

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

TextView textView = findViewById(R.id.textView);


//Handler
Handler handler = new Handler(){
public void handleMessage(Message msg){
converts = (String[])msg.obj;  ・・・➁
}
};

Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new Thread(new Runnable(){

@Override
public void run() {
// 本質的な処理
String[] texts ={"abc","def","ghi"};
//メッセージオブジェクトを利用
Message msg = new Message();
msg.what = 456;
msg.arg1 = 1234;
msg.arg2 = 5678;
msg.obj = texts;

handler.sendMessage(msg);
}
}).start();  ・・・➂

startActivity(new Intent(MainActivity.this,SubActivity.class));
}
});
}
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">


<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ここに出力" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PUSH"/>


</LinearLayout>

SubActivity.java

package com.example.roomhandler;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class SubActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sub);

ListView listView = findViewById(R.id.listView);

ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(SubActivity.this, android.R.layout.simple_list_item_1,MainActivity.converts);
listView.setAdapter(arrayAdapter);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SubActivity">

<ListView
android:id="@+id/listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

</LinearLayout>

ちなみに、コレクションのたとえば、List<String>とかでも、Messageオブジェクトのobj変数で送れます。

 

ただし、①のように、MainActivityのメンバー変数を使用するのが、androidとして、いいのかはわかりません。他の方法もあった方が良さそうですので、少し考えてみます。

Androidでは、アクティビティ間の、値の受渡しは、Intentを使います。条件付きで、参照型クラスのオブジェクトの受渡しも可能です。どのように使うか考えてみると、特定ノインタフェースを実装したクラスを新たに作成し、そのメンバー変数に目的のオブジェクトを「代入」して、コンテナ輸送みたいにしてあげればよいのかなと思います。未施行です。