Post Top Ad

Post Top Ad

Saturday 8 July 2017

Receive value from implicit intent in android



activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.kiran.lesson4_implicitintentforreceivedata.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />
</RelativeLayout>

2. MainActivity.java:
package com.example.kiran.lesson4_implicitintentforreceivedata;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

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

3.SecondActivity.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_second"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.kiran.lesson4_implicitintentforreceivedata.SecondActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="44dp"
        android:id="@+id/msg_tv"
        android:textStyle="normal|bold"
        android:textSize="36sp"
        tools:text="Name" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:ems="10"
        android:layout_below="@+id/msg_tv"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="120dp"
        android:id="@+id/age_et"
        android:hint="age" />

    <Button
        android:text="Send To First App"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="45dp"
        android:id="@+id/button"
        android:layout_below="@+id/age_et"
        android:onClick="sendBackToFirstApp"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

SecondActivity.java:

package com.example.kiran.lesson4_implicitintentforreceivedata;

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

public class SecondActivity extends AppCompatActivity {

    private TextView mMsgTv;
    private TextView mAgeEt;

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

        mMsgTv = (TextView) findViewById(R.id.msg_tv);
        Intent intent = getIntent();
        String msg = intent.getStringExtra(Intent.EXTRA_TEXT);
        mMsgTv.setText(msg);
       
    }

    public void sendBackToFirstApp(View view) {

        mAgeEt = (TextView) findViewById(R.id.age_et);
        String age = mAgeEt.getText().toString();
        Intent intent = new Intent();//Just create empty intent as we only want to return data back and not to create a new instance of MainActivity
        intent.putExtra(Intent.EXTRA_TEXT,age);
        setResult(RESULT_OK,intent);
        finish();

    }
}


No comments:

Post a Comment