Mehmet Ethem SULAN :: Android :: Android Activity startActivityForResult sample

Android içinde startActivityForResult() metodu ile başlatılan sub activitynin normal kapanıp kapanmadığına göre işlem yapabiliyoruz. Mesela devicedaki Back buttonuna basarak beklenmedik şekilde activity sonlandirabilir. Bazı durumlarda buna göre işlem yapmak zorunda kalıyoruz.

StartForResultActivity.java

package com.start.result;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class StartForResultActivity extends Activity {
	Button button1_ok;
	private static final int SHOW_SUBACTIVITY = 1;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        button1_ok=(Button) findViewById(R.id.button1);

        button1_ok.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(StartForResultActivity.this, SecondActivity.class);
				startActivityForResult(intent, SHOW_SUBACTIVITY);
			}
		});
    }
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		switch (requestCode) {
		case 1:
//Second activity normal olarak sonlanirsa RESULT_OK degeri döner.
			if(resultCode==Activity.RESULT_OK)
			{
				String string=data.getStringExtra("ethem");
				Toast.makeText(getApplicationContext(), "Inputed text: "+string, Toast.LENGTH_SHORT).show();
//Second activity beklendmedik sekilde kapanirsa(Mesela cihazdaki back buttonuna tikalnirsa) RESULT_CANCELED degeri doner.
			}else if(resultCode==Activity.RESULT_CANCELED){
				Toast.makeText(getApplicationContext(), "Beklenmedik sekilde second activity sonlandi", Toast.LENGTH_SHORT).show();
			}

			break;
		}

	}

}

SecondActivity.java

package com.start.result;

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;

public class SecondActivity extends Activity{

	Button button;
	EditText editText;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.second);
		button=(Button) findViewById(R.id.button1);
		editText=(EditText) findViewById(R.id.editText1);

		button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
//Finish buttonuna tıklanarak activity sonlanirsa(yani beklediğimiz gibi) o zaman setResult(RESULT_OK, intent); değer dönderiyoruz StartForResultActivity activiysine.
				Intent intent=new Intent();
				intent.putExtra("ethem", editText.getText().toString());
				setResult(RESULT_OK, intent);
				finish();

			}
		});

	}

}

main.xml

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

    <Button android:text="Open Second Activity" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>

second.xml

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

</LinearLayout>

Download StartForResult.zip

Write a Comment

Let me know what you think?