Home>

I want to jump from SubclassA to SubclassB by intent, not from Mainactivity, and return the value to SubclassA.

Applicable source code
<? xml version = "1.0" encoding = "utf-8"?>

    <TextView
        android: id = "@ + id/textView"
        android: layout_width = "wrap_content"
        android: layout_height = "wrap_content"
        android: text = "Hello World!"
        app: layout_constraintBottom_toBottomOf = "parent"
        app: layout_constraintLeft_toLeftOf = "parent"
        app: layout_constraintRight_toRightOf = "parent"
        app: layout_constraintTop_toTopOf = "parent" />
    <Button
        android: id = "@ + id/button"
        android: layout_width = "wrap_content"
        android: layout_height = "wrap_content"
        android: layout_marginStart = "8dp"
        android: layout_marginTop = "8dp"
        android: layout_marginEnd = "8dp"
        android: layout_marginBottom = "8dp"
        android: text = "Button"
        app: layout_constraintBottom_toBottomOf = "parent"
        app: layout_constraintEnd_toEndOf = "parent"
        app: layout_constraintHorizontal_bias = "0.517"
        app: layout_constraintStart_toStartOf = "parent"
        app: layout_constraintTop_toBottomOf = "@ + id/textView"
        app: layout_constraintVertical_bias = "0.174" />
    <Button
        android: id = "@ + id/button3"
        android: layout_width = "wrap_content"
        android: layout_height = "wrap_content"
        android: layout_marginStart = "8dp"
        android: layout_marginTop = "8dp"
        android: layout_marginEnd = "8dp"
        android: layout_marginBottom = "8dp"
        android: text = "Button"
        app: layout_constraintBottom_toBottomOf = "parent"
        app: layout_constraintEnd_toEndOf = "parent"
        app: layout_constraintHorizontal_bias = "0.517"
        app: layout_constraintStart_toStartOf = "parent"
        app: layout_constraintTop_toBottomOf = "@ + id/button"
        app: layout_constraintVertical_bias = "0.124" />
</android.support.constraint.ConstraintLayout>
package com.example.intenttest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
    private Sub sub;
    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);setContentView (R.layout.activity_main);
        Button go = findViewById (R.id.button);
        Button ret = findViewById (R.id.button3);
        sub = new Sub (MainActivity.this);
        go.setOnClickListener (new View.OnClickListener () {
            @Override
            public void onClick (View view) {
                sub.goSubb (MainActivity.this);
            }
        });
        ret.setOnClickListener (new View.OnClickListener () {
            @Override
            public void onClick (View view) {
                sub.ret (MainActivity.this);
            }
        });
    }
}
package com.example.intenttest;
import android.content.Intent;
import android.widget.TextView;
import static android.app.Activity.RESULT_OK;
class Sub {
    public static final String EXTRA_MESSAGE = "test";
    static final int RESULT_SUBACTIVITY = 1000;
    String res = "";
    Sub (MainActivity mainActivity) {
        TextView kekka = (TextView) mainActivity.findViewById (R.id.textView);
    }
    public void goSubb (MainActivity mainActivity) {
        Intent intent = new Intent (mainActivity.getApplication (), Subb.class);
        TextView kekka = (TextView) mainActivity.findViewById (R.id.textView);
        if (kekka.getText ()! = null) {
            String str = "Nothing";
            intent.putExtra (EXTRA_MESSAGE, str);
        }
        mainActivity.startActivityForResult (intent, RESULT_SUBACTIVITY);
    }
    protected void onActivityResult (int requestCode, int resultCode, Intent intent) {
        if (resultCode == RESULT_OK&&requestCode == RESULT_SUBACTIVITY&&
                null! = intent) {
            res = intent.getStringExtra (Sub.EXTRA_MESSAGE);
        }
    }
    public void ret (MainActivity mainActivity) {
        TextView kekka = (TextView) mainActivity.findViewById (R.id.textView);
        kekka.setText ("kaeri" + res);
    }
}
<? xml version = "1.0" encoding = "utf-8"?>

    <TextView
        android: id = "@ + id/textView2"
        android: layout_width = "wrap_content"
        android: layout_height = "13dp"
        android: layout_marginStart = "8dp"android: layout_marginEnd = "8dp"
        android: layout_marginBottom = "8dp"
        android: text = "TextView"
        app: layout_constraintBottom_toBottomOf = "parent"
        app: layout_constraintEnd_toEndOf = "parent"
        app: layout_constraintStart_toStartOf = "parent"
        app: layout_constraintTop_toTopOf = "parent" />
    <Button
        android: id = "@ + id/button2"
        android: layout_width = "wrap_content"
        android: layout_height = "wrap_content"
        android: layout_marginStart = "8dp"
        android: layout_marginEnd = "8dp"
        android: layout_marginBottom = "8dp"
        android: text = "Button"
        app: layout_constraintBottom_toBottomOf = "parent"
        app: layout_constraintEnd_toEndOf = "parent"
        app: layout_constraintHorizontal_bias = "0.498"
        app: layout_constraintStart_toStartOf = "parent"
        app: layout_constraintTop_toBottomOf = "@ + id/textView2"
        app: layout_constraintVertical_bias = "0.13" />
</android.support.constraint.ConstraintLayout>
package com.example.intenttest;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Subb extends MainActivity {
    private String message;
    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView (R.layout.subb);
        Intent intent = getIntent ();
        message = intent.getStringExtra (Sub.EXTRA_MESSAGE);
        final TextView textView = findViewById (R.id.textView2);
        textView.setText (message);
        // back to MainActivity
        Button button = findViewById (R.id.button2);
        button.setOnClickListener (new View.OnClickListener () {
            public void onClick (View v) {
                Intent intent = new Intent ();
                if (textView.getText ()! = null) {
                    String str = "Returned";
                    intent.putExtra (Sub.EXTRA_MESSAGE, str);
                }
                setResult (RESULT_OK, intent);
                finish ();
            }
        });
    }
}
Error message

Although it is possible to go back and forth between the Main and Sub screens by screen transition, the character string "Returned" cannot be displayed on the Main screen. I think I've made some mistakes with Intent, so I would appreciate it if you noticed it.

Surveyed by myself

We looked at various pages regarding Intent processing, but there were many transitions from MainActivity, and transitions between SubClasses were not found.

Environment

Windows10
AndroidStudio 3.4.1

  • Answer # 1

    I think that you can only pass in "intent.putExtra" when calling with startActivityForResult or startActivity.

    Below, it is an int, but it can handle String.
    If you use Preferences, you can read and write what you wrote in another Activity or Service.

    SharedPreferences sharedPreferences;
        SharedPreferences.Editor editor;
        sharedPreferences = PreferenceManager.getDefaultSharedPreferences (this);
        editor = sharedPreferences.edit ();
        // Set fragment ID
        int fragmentId = 1;
        editor.putInt ("fragmentId", fragmentId);
        editor.commit ();
        // Read fragment ID
        fragmentId = sharedPreferences.getInt ("fragmentId", 0);