-1

I have a Parent Activity and a Child Activity. The Child Activity has no toolbar so can you not navigate back. You can only go back to the Parent Activity if the back key is pressed or when a certain button is pressed (I use finish() in this case). However, I have to know if the user did press that button, but how can I give data to the Parent Activity from the Child Activity?

tomhogenkamp
  • 83
  • 1
  • 11
  • create a java class to hold that data it will act as like MAN IN THE MIDDLE child activity must keep that data into that class before terminating then parent activity read that class to get the data lolz – Zain Ul Abidin Jun 16 '17 at 16:38

2 Answers2

0

Start your child Activity with startActivityForResult. Then you can return a result to the starting (parent) Activity

F43nd1r
  • 7,690
  • 3
  • 24
  • 62
0

In ActivityOne we have two elements 1)Button to open child activity 2)TextView to bind the results from child activity

In Activity Two we have two elements 1)EditText to enter data which need to send back to parent activty 2)button to send the data entered in edittext

please check the below sample code for refference

Parent Activity : ActivityOne.java

    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;

    /**
     * Created by jaffer on 16/6/17.
     */

    public class ActivityOne extends AppCompatActivity implements View.OnClickListener {

        public static final String DATA = "data";
        private static final int REQUEST_CODE = 1001;

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

        private void initViews() {
            ((Button) findViewById(R.id.btn_one)).setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            Intent i = new Intent(this, ActivityTwo.class);
            startActivityForResult(i, REQUEST_CODE);
        }

        @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK && data != null) {
                if (data.getExtras().containsKey(DATA)) {
                    updateUI(data.getExtras().getString(DATA));
                }
            }
        }

        private void updateUI(String string) {
            ((TextView) findViewById(R.id.tv_result)).setText(string);
        }
    }

parent layout activity_one.xml

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

    <Button
        android:id="@+id/btn_one"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="open child activty"/>

    <TextView
        android:id="@+id/tv_result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"/>
</LinearLayout>

Child Activity : ActivityTwo.java

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

/**
 * Created by jaffer on 16/6/17.
 */

public class ActivityTwo extends AppCompatActivity implements View.OnClickListener {


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

    private void initViews() {
        ((Button) findViewById(R.id.btn_send_result)).setOnClickListener(this);
    }


    @Override
    public void onClick(View view) {
        Intent returnIntent = new Intent();
        returnIntent.putExtra(ActivityOne.DATA, ((EditText) findViewById(R.id.et_result)).getText().toString());
        setResult(Activity.RESULT_OK, returnIntent);
        finish();
    }
}

child layout activity_one.xml

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

    <EditText
        android:id="@+id/et_result"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"/>

    <Button
        android:id="@+id/btn_send_result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Send data"/>
</LinearLayout>
Jafar Sadiq SH
  • 715
  • 9
  • 22