Pages

Tuesday, September 20, 2016

Android Example- Explicit Intent


activity_main.xml
<RelativeLayout 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: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=".MainActivity"
    android:background="@drawable/background" >

    <TextView
        android:id="@+id/main_textview_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Main Activity to launch other activity via Intent Explicitily"
        android:textSize="30dp" >
    </TextView>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/main_textview_1"
        android:layout_alignRight="@+id/main_textview_1"
        android:layout_centerVertical="true"
        android:text="Launch Activity1" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button1"
        android:layout_alignParentBottom="true"
        android:layout_alignRight="@+id/main_textview_1"
        android:layout_marginBottom="72dp"
        android:text="Launch Activity2" />

</RelativeLayout>

MainActivity.java
package in.kantapp.example32;

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

public class MainActivity extends AppCompatActivity implements OnClickListener {

    private static final int REQUEST_CODE1 = 1;
    private static final int REQUEST_CODE2 = 2;
    private Intent intent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button b1 = (Button) findViewById(R.id.button1);
        Button b2 = (Button) findViewById(R.id.button2);
        b1.setOnClickListener(this);
        b2.setOnClickListener(this);
    }



    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
            case R.id.button1:
                intent = new Intent(this, Activity1.class);
                intent.putExtra("Value1", "This value one for Activity1 ");
                intent.putExtra("Value2", "This value two Activity1");
                startActivityForResult(intent, REQUEST_CODE1);
                break;
            case R.id.button2:
                Intent intent = new Intent(this, Activity2.class);
                intent.putExtra("Value1", "This value one for Activity2");
                intent.putExtra("Value2", "This value two Activity2");
                startActivityForResult(intent, REQUEST_CODE2);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        if (resultCode == RESULT_OK && requestCode == REQUEST_CODE1) {
            if (data.hasExtra("returnKey1")) {
                Toast.makeText(this, data.getExtras().getString("returnKey2"),
                        Toast.LENGTH_SHORT).show();
            }
        }
        if (resultCode == RESULT_OK && requestCode == REQUEST_CODE2) {
            if (data.hasExtra("returnkey2")) {
                Toast.makeText(this, data.getExtras().getString("returnkey1"),
                        Toast.LENGTH_LONG).show();
            }
        }

    }
}

Activity1.java
package in.kantapp.example32;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Activity1 extends Activity implements OnClickListener {

    private String str1, str2;
    private EditText text1, text2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout1);
        Bundle data = getIntent().getExtras();
        if (data == null) {
            return;
        }
        String value1 = data.getString("Value1");
        String value2 = data.getString("Value2");
        if (value1 != null && value2 != null) {
            text1 = (EditText) findViewById(R.id.editText1);
            text2 = (EditText) findViewById(R.id.editText2);
            Button but = (Button) findViewById(R.id.layout_button1);
            but.setOnClickListener(this);
            str1 = text1.getText().toString();
            str2 = text2.getText().toString();
            TextView textview1 = (TextView) findViewById(R.id.textView1);
            TextView textview2 = (TextView) findViewById(R.id.textView2);
                      /*
                       * the text view displays data which are sent to the activity via
                       * intent
                       */
            textview1.setText(value1);
            textview2.setText(value2);
        }
    }

    @Override
    public void onClick(View view) {
        finish();
    }

    @Override
    public void finish() {
        Intent send_data = new Intent();
        send_data.putExtra("returnKey1", "Returned return key1 from Activity1");
        send_data.putExtra("returnKey2", "Returned return key2 from Activity1");
        setResult(RESULT_OK, send_data);
        super.finish();
    }
}

Activity2.java
package in.kantapp.example32;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Activity2 extends Activity implements OnClickListener {

    private String str1, str2;
    private EditText text1, text2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout1);

        Bundle data = getIntent().getExtras();
        if (data == null) {
            return;
        }
        String value1 = data.getString("Value1");
        String value2 = data.getString("Vlaue2");
        Button b1 = (Button)findViewById(R.id.layout_button1);
        b1.setOnClickListener(this);

        if (value1 != null && value2 != null) {
            text1 = (EditText) findViewById(R.id.editText1);
            text2 = (EditText) findViewById(R.id.editText2);
            Button but = (Button) findViewById(R.id.layout_button1);
            but.setOnClickListener(this);

            str1 = text1.getText().toString();
            str2 = text2.getText().toString();

            TextView textview1 = (TextView) findViewById(R.id.textView1);
            TextView textview2 = (TextView) findViewById(R.id.textView2);
                     /*
                      * the text view displays data which are sent to the activity via
                      * intent
                      */
            textview1.setText(value1);
            textview2.setText(value2);

        }
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        finish();
    }

    @Override
    public void finish() {
        Intent send_data = new Intent();
        send_data.putExtra("returnKey1", "Returned return key1 from Activity2");
        send_data.putExtra("returnKey2", "Returned return key2 from Activity2");
        setResult(RESULT_OK, send_data);
        super.finish();
    }

}

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:layout_marginTop="15dp"
        android:text="TextView"
        android:textSize="20dp" />
    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="34dp"
        android:ems="10">

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:layout_marginTop="20dp"
        android:text="TextView"
        android:textSize="20dp" />



    <EditText
        android:id="@+id/editText2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:ems="10" />

    <Button
        android:id="@+id/layout_button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:text="Click To Finish" />

</LinearLayout>

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="in.kantapp.example32">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".Activity1"/>
        <activity android:name=".Activity2"/>
    </application>

</manifest>

No comments:

Post a Comment