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 myselfWe looked at various pages regarding Intent processing, but there were many transitions from MainActivity, and transitions between SubClasses were not found.
EnvironmentWindows10
AndroidStudio 3.4.1
-
Answer # 1
Related articles
- where is the defaultrealm when using realm on android stduio?
- i want to develop an android game application using c++ in visual studio
- android - i want to use an intent from a fragment
- java - how to be sure to open the system file app in android intent
- jquery ui - i don't know how to save when moving data between data lists using sortable
- visual studio - i want to make an android application using xamarin
- android - i want to be able to adjust the color of the image using seekbar
- python - i do not understand the difference between using () and not using method
- python - fill the part between the two functions using sympy
- java - i want to play youtube on android using the youtube android player api
- i want to make a login screen using android x
- android studio - how should i write this part of intent in countdowntimer?
- passing values using bundle between android fragments
- android - about data sharing between scenes using unity scriptable object
- i don't know about intent in android studio (java)
- posting between books using vba macros
- differences in commands between ios and android when building apk with flutter and how to specify the production file
- python - [django] i want to exchange data between apps in the project
- flutter android apk processing differs between debug and release
- c # - i can't bind using relativesource when i put contextmenu in between
Related questions
- android - i want to do something (toast, activity transition, etc) at 0:00 on kotlin
- java - i want to change the screen when clicking a button in android studio
- [java] [android studio] screen transition method
- java - problem when displaying alertdialog when starting android (app) and ending the app
- java environment variable error [java]
- java - about library introduction in android studio
- java - i don't want to duplicate questions that are displayed with random numbers
- android - i want to divide animation into different classes [flutter]
- android - animated builder does not work [flutter]
- android - constraints in basic activity [kotlin]
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.