简体   繁体   中英

Modify Text View values after clicking a button from another activity

I've been trying the alter the value of a TextView field by clicking from another activity. I tried the various Activity Lifecycle methods. The onRestart() method changes the TextView value when I press the back button but what I want is to change that Value on Button click from another activity.

Here is my MainActivty

package com.example.activity1;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.pm.LauncherActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    TextView textView;
    Button submitButton;
    Context context;

    @Override
    protected void onRestart() {
        super.onRestart();
        TextView textView = findViewById(R.id.text1);
        textView.setText("Welcome Back");

        Toast.makeText(context, "Restart Started", Toast.LENGTH_SHORT).show();
    }


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

        textView = findViewById(R.id.text1);
        submitButton = findViewById(R.id.submit);
        context = this;

        submitButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(context, Activity2.class));
            }
        });


    }
}

My Second Activity Class

    package com.example.activity1;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class Activity2 extends AppCompatActivity {

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

        Context context;
        Button reverseButton;

        reverseButton = findViewById(R.id.reverse);
        context = this;

        reverseButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, MainActivity.class);


                startActivity(intent);
            }
        });
    }
}

my Activity2.xml

    <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".Activity2">

    <TextView
        android:id="@+id/textt2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Activity 2"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/reverse"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        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_toTopOf="parent"
        app:layout_constraintVertical_bias="0.592" />
</androidx.constraintlayout.widget.ConstraintLayout>

And my Main_Activity.xml

    <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Main Page"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/submit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Submit"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.628" />

</androidx.constraintlayout.widget.ConstraintLayout>

You should pass something from Activity 2 to Activity 1 to tell Activity 1 that you already were here. Since you use Intents putExtra to do this

Intent intent = new Intent(context, MainActivity.class);
String killroyWasHere= "Welcome Back";
intent.putExtra("iAmBack", killroyWasHere);
startActivity(intent);

and in Activity 1 in OnCreate()

@Override
protected void onCreate(Bundle savedInstanceState) {
  ...
    String myData = getIntent().getStringExtra("iAmBack");
    if (myData != null) {
        TextView textView = findViewById(R.id.text1);
        textView.setText(myData);
        Toast.makeText(context, "Restart Started", Toast.LENGTH_SHORT).show();
    }
   ...
}

Should work,didn't used Java in Android for ages.

Hope i understand correctly what you wanted to do and this was helpful answer

For more reading suggest Intents and Intent Filters

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM