How to print toast on 2nd activity? [closed]

I am trying to print number of lines in toast. but it just puts me in the first activity again and again.

idk what I am doing wrong here.

The code is supposed to do like: -> enter number in text box and it will print no. of lines in the toast on 2nd activity when the button in pressed.

code is like this:

import androidx.appcompat.app.AppCompatActivity;

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

public class MainActivity extends AppCompatActivity {

    private Button obbtSend;
    private EditText obetMessage;

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

        obbtSend = (Button)findViewById(R.id.btSend);
        obetMessage = (EditText)findViewById(R.id.etMessage);

        obbtSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openMainActivity();
            }
        });
    }

    public void openMainActivity(){
        String str = obetMessage.getText().toString();
        Intent obintent = new Intent(this, MainActivity.class);
        obintent.putExtra("var", str);
        this.startActivity(obintent);
    }
}

on the 2nd file:

package com.example.toastexample;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

import org.w3c.dom.Text;

public class MainActivity2 extends AppCompatActivity {

    private TextView obtvMsg;

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

        Intent ob = getIntent();
        String msg = "";
        String s = ob.getStringExtra("var").toString();
        int t = Integer.parseInt(s.toString());
        for(int i=1;i<=t;i++)
            msg=msg + "Hello World! n";
        Toast.makeText(this,msg,Toast.LENGTH_LONG).show();

        obtvMsg = (TextView)findViewById(R.id.tvMsg);
        obtvMsg.setText(ob.getStringExtra("var").toString());
    }
}

Answer

You didn’t start the MainActivity2 but the MainActivity self again in openMainActivity(),

use code below to start the MainActivity2.

public void openMainActivity(){
        String str = obetMessage.getText().toString();
        Intent obintent = new Intent(this, MainActivity2.class);
        obintent.putExtra("var", str);
        this.startActivity(obintent);
}