Check Activity result and onActivityResult : Activity « UI « Android






Check Activity result and onActivityResult

     


package app.test;

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;
 class Activity1 extends Activity {
  OnClickListener listener1 = null;
  Button button1;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.row);
        listener1 = new OnClickListener() {
      public void onClick(View v) {
        Bundle bundle = new Bundle();
        bundle.putString("store", "Activity1");
        Intent mIntent = new Intent();
        mIntent.putExtras(bundle);
        setResult(RESULT_OK, mIntent);
        finish();
      }
    };
    button1 = (Button) findViewById(R.id.button3);
    button1.setOnClickListener(listener1);
    String data=null;
     Bundle extras = getIntent().getExtras();
          if (extras != null) {
               data = extras.getString("activityMain");
          }
    setTitle("Activity1:"+data);
    }
}

 class Activity2 extends Activity {
  OnClickListener listener = null;
  Button button;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_edit);
        listener = new OnClickListener() {
      public void onClick(View v) {
        finish();
      }
    };
    button = (Button) findViewById(R.id.button4);
    button.setOnClickListener(listener);
    setTitle("Activity2");
    }
}
public class Test extends Activity {
  OnClickListener listener1 = null;
  OnClickListener listener2 = null;
  Button button1;
  Button button2;
  static final int REQUEST_CODE = 1;
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    listener1 = new OnClickListener() {
      public void onClick(View v) {
        Intent intent1 = new Intent(Test.this, Activity1.class);
        intent1.putExtra("activityMain", "activityMain");
        startActivityForResult(intent1, REQUEST_CODE);
      }
    };
    listener2 = new OnClickListener() {
      public void onClick(View v) {
        setTitle("ActivityMain");
        Intent intent2 = new Intent(Test.this, Activity2.class);
        startActivity(intent2);

      }
    };
    setContentView(R.layout.main);
    button1 = (Button) findViewById(R.id.button1);
    button1.setOnClickListener(listener1);
    button2 = (Button) findViewById(R.id.button2);
    button2.setOnClickListener(listener2);
    setTitle("ActivityMain");
  }
    
    @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_CODE) {
      if (resultCode == RESULT_CANCELED)
        setTitle("OK");
      else if (resultCode == RESULT_OK) {
        String temp=null;
         Bundle extras = data.getExtras();
              if (extras != null) {
                temp = extras.getString("store");
              }
        setTitle(temp);
      }
    }
  }
}

//  layout/main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical" android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  <Button android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:text="??button1" />
  <Button android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:text="??button2" />
</LinearLayout>


//  layout/row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical" android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  
  <Button android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:text="??button3" />

</LinearLayout>



//  layout/add_edit.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical" android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  
  <Button android:id="@+id/button4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:text="??button4" />
</LinearLayout>

   
    
    
    
    
  








Related examples in the same category

1.Backup Activity
2.Notification Activity
3.Timing Activity
4.Set content view from xml for Activity
5.More than one Activity
6.Find user control by using findViewById
7.A Simple Form
8.Link form with POJO
9.Life cycle
10.Comparing Android UI Elements to Swing UI Elements
11.add android:background = "#FFFF0000"
12.Rotation demo
13.Set activity screen Orientation
14.Check activity result
15.Activity lifecycle event
16.Activity key event
17.Allows the activity to manage the Cursor's lifecyle based on the activity’s lifecycle---
18.Activity configuration changed event
19.Phone Call Activity
20.Using Media Store Activity
21.External Storage Activity
22.Making Activity Go Full-Screen
23.Surface View Test Activity
24.Widget Preview Activity
25.This class provides a basic demonstration of how to write an Android activity.
26.This demonstrates the basic code needed to write a Screen activity
27.Example of removing yourself from the history stack after forwarding to another activity.
28.Fancy Blur Activity
29.Example of receiving a result from another activity.
30.Demonstrates required behavior of saving and restoring dynamic activity state
31.Securer Activity
32.This activity is an example of a simple settings screen that has default values.
33.Bind Click Action Activity
34.get ActivityManager
35.forward to another Activity
36.Request window features before setContentView
37.No code required here to attach the listener
38.Save data to Preference
39.Save instance state
40.onRetainNonConfigurationInstance
41.Create a user interface in code.
42.Redirect
43.Demonstrates how the various soft input modes impact window resizing.
44.extends Activity implements OnClickListener