-2

`

MainActivity
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:background="@color/colorAccent"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity">


        <TextView
            android:layout_marginTop="30dp"
            android:textStyle="bold"
            android:textColor="#fff"
            android:id="@+id/textView"
            android:textSize="22sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="What is your name?" />

        <EditText
            android:id="@+id/name"
            android:layout_width="250dp"
            android:layout_height="wrap_content" />

        <Button
            android:id="@+id/btn1"
            android:layout_width="250dp"
            android:layout_height="wrap_content"
            android:text="Next"
            android:layout_marginTop="200dp"
            android:layout_gravity="center_horizontal"/>


<Button
            android:id="@+id/btnSummary"
            android:layout_width="250dp"
            android:layout_height="wrap_content"
            android:text="Open Summary"
            android:layout_marginTop="200dp"
            android:layout_gravity="center_horizontal"/>

    </LinearLayout>
   // SecondActitvity
   

 <?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"
        android:background="#8E44AD"
        android:orientation="vertical"
        tools:context=".SecondActivity">

        <TextView
            android:layout_marginTop="30dp"
            android:textStyle="bold"
            android:textColor="#fff"
            android:id="@+id/textView"
            android:textSize="22sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="Who is the best cricketer in the world?" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_marginTop="80dp">


            <RadioGroup
                android:id="@+id/options"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" >


            <RadioButton
            android:id="@+id/jacques"
            android:paddingLeft="15dp"
            android:textColor="#fff"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Jacques Kallis"
            android:textSize="22sp"/>

        <RadioButton
            android:id="@+id/adnan"
            android:paddingLeft="15dp"
            android:textColor="#fff"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Adnan"
            android:textSize="22sp"/>


        <RadioButton
            android:id="@+id/sachine"
            android:paddingLeft="15dp"
            android:textColor="#fff"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Sachine Tendulkar"
            android:textSize="22sp"/>

        <RadioButton
            android:id="@+id/virat"
            android:paddingLeft="15dp"
            android:textColor="#fff"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Virat"
            android:textSize="22sp"/>
            </RadioGroup>
        </LinearLayout>
        <Button
            android:id="@+id/btn2"
            android:layout_width="250dp"
            android:layout_gravity="center"
            android:layout_height="wrap_content"
            android:layout_marginTop="150dp"
            android:background="#ECF0F1"
            android:text="Next"
            />
    </LinearLayout>
//ThirdActivity
   

 <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="#7845"
        tools:context=".ThirdActivity">
        <TextView
            android:layout_marginTop="30dp"
            android:textStyle="bold"
            android:id="@+id/textView"
            android:textSize="22sp"
            android:textColor="#fff"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="What are the colors in indian national flag?" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#fff"
            android:layout_marginTop="10dp"
            android:text="Options:"
            android:textStyle="bold"
            android:textSize="22sp"
            android:paddingLeft="10dp"/>


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="15dp"
            android:layout_marginTop="10dp"
            android:orientation="vertical">

        <CheckBox
            android:id="@+id/checkBox"
            android:padding="5dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clickable="true"
            android:textColor="#fff"
            android:textSize="22sp"
            android:text="White" />

        <CheckBox
            android:id="@+id/checkBox2"
            android:padding="5dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="#fff"
            android:textSize="22sp"
            android:text="Orage" />

        <CheckBox
            android:textSize="22sp"
            android:padding="5dp"
            android:id="@+id/checkBox3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="#fff"
            android:text="Green" />

        <CheckBox
            android:textSize="22sp"
            android:padding="5dp"
            android:id="@+id/checkBox4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="#fff"
            android:text="Yellow" />

            <Button
                android:id="@+id/btn3"
                android:layout_width="250dp"
                android:layout_gravity="center"
                android:layout_height="wrap_content"
                android:layout_marginTop="150dp"
                android:background="#ECF0F1"
                android:text="Next"
                />

    </LinearLayout>
    </LinearLayout>
//SummaryActivity
   

 <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:background="@color/colorAccent"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:id="@+id/textMsg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Name"
            android:textSize="22sp"
            android:textColor="#fff"
            android:textStyle="bold"
            android:layout_marginTop="30dp"
            android:layout_marginLeft="20dp"/>
    </LinearLayout>
//MainActivity.java


public class MainActivity extends AppCompatActivity {

Button button;
TextView tv;
EditText editText;
Context context;
Button buttonSummary;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tv=findViewById(R.id.textMsg);
        editText = findViewById(R.id.name);
        button = findViewById(R.id.btn1);
        buttonSummary = findViewById(R.id.btnSummary);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getApplicationContext(), SecondActivity.class));
            }
        });

        buttonSummary.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                openSummaryActivity();
            }
        });
    }
    public void openSummaryActivity() {
        String text = editText.getText().toString();
        Intent intent = new Intent(getApplicationContext(), SummaryActivity.class);
        intent.putExtra("KEY", text);
        startActivity(intent);
    }}

SummaryActivity.java

public class SummaryActivity extends AppCompatActivity {


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

        Intent intent = getIntent();
        String text = intent.getStringExtra("KEY");
        TextView textView = (TextView) findViewById(R.id.textMsg);
        textView.setText(text);

    }
}

`I have 4 activities.First activity with input text.

Second activity with selecting best cricketer, used radio buttons.

Third activity selecting options used checkbox.

In fourth activity I need to display everything from activity 1 2 3's selected data in 4th activity.

SUMMARY

Hello “Name:” ,

Here are the answers selected:

Who is the best cricketer in the world?

Answer: “ “

What are the colors in the national flag?

Answers : “Commo separated”

Muazzam A.
  • 647
  • 5
  • 17
  • Please post the code you have tried so far. – Ashish Sep 11 '19 at 11:25
  • 3
    Possible duplicate of [How do I pass data between Activities in Android application?](https://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android-application) – a_local_nobody Sep 11 '19 at 11:33

3 Answers3

0

You should use Singletone class like this,

public class InfoSingleton {
private static final InfoSingleton ourInstance = new InfoSingleton();
private String name;
private String bestCricketer;

public static InfoSingleton getInstance() {
    return ourInstance;
}

private InfoSingleton() {
}

public static InfoSingleton getOurInstance() {
    return ourInstance;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getBestCricketer() {
    return bestCricketer;
}

public void setBestCricketer(String bestCricketer) {
    this.bestCricketer = bestCricketer;
}
}
hasan_shaikh
  • 1,434
  • 1
  • 15
  • 38
0

You can pass data from one activity to another using intents.

Pass data from Activity1 to Activity2

public class Activity1 extends Activity {

// write this code on button click or event that moving from Activity1 to Activity2
Intent intent = new Intent(Activity1.this, Activity2.class);
intent.putExtra("ans1", ans1Value);
startActivity(intent);

}

Access data in Activity2 and pass to Activity3

    public class Activity2 extends Activity {
     @Override
        protected void onCreate(Bundle savedInstanceState) {
    String ans1Value = getIntent().getStringExtra("ans1"); //Answer from Activity1 
    }

// write this code on button click or event that moving from Activity2 to Activity3

 Intent intent = new Intent(Activity2.this, Activity3.class);
    intent.putExtra("ans1", ans1Value);
    intent.putExtra("ans2", ans2Value);
    startActivity(intent);

    }

Access data in Activity3 and pass to Activity4

public class Activity3 extends Activity {
         @Override
            protected void onCreate(Bundle savedInstanceState) {
        String ans1Value = getIntent().getStringExtra("ans1");
        String ans2Value = getIntent().getStringExtra("ans2");  //Answer from Activity2 
        }

    // write this code on button click or event that moving from Activity3 to Activity4

     Intent intent = new Intent(Activity3.this, Activity4.class);
        intent.putExtra("ans1", ans1Value);
        intent.putExtra("ans2", ans2Value);
        intent.putExtra("ans3", ans3Value); //Answer from Activity3 
        startActivity(intent);

        }

Show Data in Fourth activity

public class Activity4 extends Activity {
             @Override
                protected void onCreate(Bundle savedInstanceState) {
            String ans1Value = getIntent().getStringExtra("ans1");
            String ans2Value = getIntent().getStringExtra("ans2");
            String ans3Value = getIntent().getStringExtra("ans3");
            //Show this information on Fourth Activity4
            }
}
Muazzam A.
  • 647
  • 5
  • 17
  • I have updated my code.Still not working.I want text should be passed to the summary activity. – jabeen akkalkot Sep 12 '19 at 06:57
  • What issue you are facing please explain – Muazzam A. Sep 12 '19 at 11:10
  • @jabeenakkalkot I have edited your code, now when you click on summary button in MainActivity it will get the text from EditText and show in SummaryActivity. This is just for example so that your code start working. The reason why your code is not working because you have set click listener on EditText and when you click on EditText it's move to SummaryActivity with Empty string and SummaryActivity have nothing to show. – Muazzam A. Sep 12 '19 at 11:23
  • First of all i dont have summary button in MainActivity.In MainActivity i have one textview with question what is your name and edit text from where i need to pass my text to last activity that is summary activity and next button to move to second activity.In MainActivity i have added my code:Intent intent = new Intent(getApplicationContext(), SecondActivity.class); String text=editText.getText().toString(); intent.putExtra("KEY", text); startActivity(intent); Here is my summary activity code: Intent intent = getIntent(); String text = intent.getStringExtra("KEY");textView.setText(text); – jabeen akkalkot Sep 12 '19 at 12:07
  • Yes I know it's just for example, in your previous code you have put the click listener on EditText which is wrong you should handle it using button or using keyboard event. – Muazzam A. Sep 12 '19 at 12:13
  • you have used my code? In your code it will show "Hello null" – Muazzam A. Sep 12 '19 at 12:23
  • Define a button in MainActivity xml and write this code in MainActivity buttonSummary = findViewById(R.id.btnSummary); buttonSummary.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { openSummaryActivity(); } }); } – Muazzam A. Sep 12 '19 at 13:00
  • This worked for me.Thanks! Is it same for radio buttons and checkbox also? – jabeen akkalkot Sep 12 '19 at 16:54
  • @jabeenakkalkot glad to hear that! also don't forget to accept my answer. – Muazzam A. Sep 12 '19 at 17:03
  • Hey,One more doubt I have is it possible to add multiple listeners on same button?Coz I want to move on clicking same button to next activity also to pass data to last activity. – jabeen akkalkot Sep 12 '19 at 17:29
  • You can add one listener at a time on one button – Muazzam A. Sep 12 '19 at 17:47
-1

you can pass data between activities using Intents

Intent intent = new Intent(Activitya.this, Activityb.class);
        intent.putExtra("NAME", "your value");
        startActivity(intent);

On next activity

String name= getIntent().getStringExtra("NAME");
Emad Seliem
  • 608
  • 1
  • 4
  • 5