开发者

onActivityResult works for first only problem

开发者 https://www.devze.com 2023-03-01 01:16 出处:网络
hi all i am trying to get values from second activity in first(main) activity. for that i use onActivityResult in firstactivity. it return value to first(main) activity from second activity first time

hi all i am trying to get values from second activity in first(main) activity. for that i use onActivityResult in firstactivity. it return value to first(main) activity from second activity first time only. again i am come to firstactivity the previous value only shown.

my application flow:

first->second->first

first(main)activity:

   public class main extends Activity {
    /** Called when the activity is first created. */
    LinearLayout l2;
    TextView tv2;
    String check=null;
//    static int value=0;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        l2 = (LinearLayout)findViewById(R.id.linearLayout2);
        tv2=(TextView)findViewById(R.id.textView2);


    l2.setClickable(true);

    l2.setOnClickListener(new View.OnClickListener() 
    {
        public void onClick(View view) 
        {
            Intent myIntent = new Intent(view.getContext(), secondactivity.class);
             startActivityForResult(myIntent, 0);
        }

    });

}

@Override
public void onActivityResult(int reqCode, int resCode, Intent data)
{
    Log.e("runapp1","onActivityResult");
    Bundle bundle = data.getExtras();
    check = bundle != null ? bundle.getString("key1") : "";
    Log.e("runapp1","onActivityResult value  "+check);
    tv2.setText(check);
}    
}

in second activity:

public class secondactivity extends Activity {
    /** Called when the activity is first created. */
    CheckBox c1,c2;
    TextView t;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.secondactivity);
        c1=(CheckBox)findViewById(R.id.checkBox1);
        c2=(CheckBox)findViewById(R.id.checkBox2);

    c1.setOnClickListener(new View.OnClickListener() 
    {
        public void onClick(View view) 
        {
            c1.setChecked(true); 
            Bundle bundle = new Bundle();
            bundle.putString("key1",String.valueOf(c1.getText()));
            Intent myIntent = new Intent(view.getContext(), main.class);
            myIntent.p开发者_StackOverflowutExtras(bundle);
            setResult(RESULT_OK, myIntent);
            finish();

        }

    });
    c2.setOnClickListener(new View.OnClickListener() 
    {
        public void onClick(View view) 
        {
            c2.setChecked(true); 
            Bundle bundle = new Bundle();
            bundle.putString("key1",String.valueOf(c1.getText()));
            Intent myIntent = new Intent(view.getContext(), main.class);
            myIntent.putExtras(bundle);
            setResult(RESULT_OK, myIntent);
            finish();               
        }

    });

}
}

please assist me.


You need to do the following, depending on where you want to capture your checkbox's value.

c1.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
    {
        if (c1.isChecked())
        {
            Bundle bundle = new Bundle();
            bundle.putString("key1",String.valueOf(c1.getText()));
            Intent myIntent = new Intent();
            myIntent.putExtras(bundle);
            setResult(RESULT_OK, myIntent);
            finish();
        }
    }
});
0

精彩评论

暂无评论...
验证码 换一张
取 消