android - How to use intent startactivityforresult method? -
i created library, , library has 4 activity. move between theese activitys intent, dont know how send result data, main application? start library this:
intent intent = new intent(this, libraryfirstactivity.class); startactivityforresult(intent, 1); @override protected void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); if (requestcode == 1){ if (resultcode == result_ok){ string result = data.getstringextra("result"); log.d("result", result); } if (resultcode == result_canceled){ toast.maketext(this, "canceled", toast.length_short).show(); } } }
i start libraryfirstactvity. in libraryfirstactivity go librarysecondactivity:
intent intent = new intent(this, librarysecondactivity.class); startactivity(intent); finish();
but can go librarythirdactivity too, or other. how can return main app onactivityresult()
method?
1) firstactivity call secondactivity using startactivityforresult()
method.
for example:
intent = new intent(this, secondactivity.class); startactivityforresult(i, 1);
2) in secondactivity set data want return firstactivity. if don't want return back, don't set any.
for example: in secondactivity if want send data:
intent returnintent = new intent(); returnintent.putextra("result", result); setresult(result_ok,returnintent); finish();
the result can of type : string / boolean / int / long / bundle ...
if don't want return data:
intent returnintent = new intent(); setresult(result_canceled, returnintent); finish();
3) in firstactivity class write following code onactivityresult()
method.
protected void onactivityresult(int requestcode, int resultcode, intent data) { if (requestcode == 1) { if(resultcode == result_ok){ string result=data.getstringextra("result"); } if (resultcode == result_canceled) { //write code if there's no result } } }//onactivityresult
Comments
Post a Comment