I'm working on a project with android 1.6 as target.
So, I can't use
#Override public void onBackPressed(){...}
I'm starting an activity with
startActivityForResult(intent,requestcode)
And I wan't to get back some info with
#Override protected void onActivityResult (int requestCode, int resultCode, Intent data)
the problem is the following:
-'Activity A' throws 'Activity B'
-during 'ActivityB' the hardware back button is pressed
-'Activity A's onActivityResult is called but I don't get any info in data (data==null)
I'm trying to put some extra info at 'Activity B's
#Override protected void onPause(){...}
I also call setResult(RESULT_OK,i); into this onPause but I always get RESULT_CANCELED and data==null at 'Activity A's onActivityResult
Instead of onBackPressed you can use:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
//set result and finish()
} else {
return super.onKeyDown(keyCode, event);
}
}
I don't see the values of the constants you pass to and fro. And they are important.
Here are some pieces of my working code that calls an activity for result:
------------------------------------- PackVideo activity --------------------------------------
calling for ServerSetActivity
Intent serverSetIntent = new Intent();
serverSetIntent.setClass(this, ServerSetActivity.class);
startActivityForResult(serverSetIntent, CHANGE_IP);
constants setting and result catching: (I have them together, because only here in catching both costants meet)
static public int CHANGE_IP = 1000;
static public int CHANGE_IP_DONE = 1001;
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// If the request was for CHANGE_IP and the request was CHANGE_IP
if (resultCode == CHANGE_IP_DONE && requestCode == CHANGE_IP) {
readBaseInfoFromServer(getApplicationContext());
startVideoPlayerService(getApplicationContext());
setCurrentChannelAndPlay(getApplicationContext(), 0);
}
}
----------------------------------- ServerSetActivity ----------------------------------------
ending of the called activity
final Intent intent = new Intent();
setResult(PackVideo.CHANGE_IP_DONE, intent);
finish();
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// check if the request code is same as what is passed here it is 2
if (resultCode != RESULT_OK) {
if (requestCode == 2 && data != null) {
//DO YOUR OVER HERE}
Related
i have this fragment which makes a call to an activity using start activity for result. this activity is supposed to show a lock pattern. the problem is that the 'onActivityResult' is never called. I put some toasts to check but it never gets printed.
public class int_Create_Pattern extends Fragment {
private static final int REQ_CREATE_PATTERN = 1;
#Override
public void onStart() {
// TODO Auto-generated method stub
super.onStart();
LockPatternView.MATRIX_WIDTH = 4;
Intent intent = new Intent(LockPatternActivity.ACTION_CREATE_PATTERN,
null, getActivity().getBaseContext(), LockPatternActivity.class);
startActivityForResult(intent, REQ_CREATE_PATTERN);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(resultCode, resultCode, data);
switch (requestCode) {
case REQ_CREATE_PATTERN: {
if (resultCode == LockPatternActivity.RESULT_OK) {
char[] pattern = data
.getCharArrayExtra(LockPatternActivity.EXTRA_PATTERN);
DataBaseHandler handler = new DataBaseHandler(getActivity()
.getApplicationContext());
handler.open();
String PatternToWrite = new String(pattern);
handler.createPattern(PatternToWrite);
handler.close();
Log.d("DEBUG", new String(pattern));
Toast.makeText(getActivity().getApplicationContext(),
"Pattern Recorded", Toast.LENGTH_LONG).show();
}
if (resultCode == LockPatternActivity.RESULT_CANCELED) {
Toast.makeText(getActivity().getApplicationContext(),
"Pattern Cancelled", Toast.LENGTH_LONG).show();
}
break;
}// REQ_CREATE_PATTERN
}
}
}
onActivityResult should be an Activity method, and not be on a Fragment.
Another problem is that you shouldn't call a new Activity from a Fragment. Implement an Interface for comunication, as described by the link bellow
http://developer.android.com/training/basics/fragments/communicating.html
The activity that holds your fragment should be the responsable for calling the new activity, and processing the result.
In Parent Class: do this code
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.dualPane);
fragment.onActivityResult(requestCode, resultCode, data);
}
In Child Class:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//in fragment class callback
}
Here's my code:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == LOGIN_REQUEST) {
if(resultCode == RESULT_OK) {
//Do Stuff
} else if(resultCode == RESULT_CANCELED) {
moveTaskToBack(true);
}
return;
}
super.onActivityResult(requestCode, resultCode, data);
}
After moveTaskToBack(true) my app is backgrounded, but onActivityResult gets called every time I try to open my app, which immediately backgrounds it again. How do I signal that I've handled the activity result and don't want to be notified about it again?
It should be noted, that I tried this with
super.onActivityResult(requestCode, resultCode, data);
being the first thing in the method as well, same problem.
You probably need something like this, instead of doing it on the onActivityResult method:
#Override
public void onBackPressed() {
moveTaskToBack(true);
}
I have been fighting with this problem for hours now and have looked at pretty much every other post about this issue but can't find where I am going wrong. Please let me know if I am missing something simple.
Activity 1 ( hosts a CreateFragment)
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
((CreateFragment) getFragmentManager().findFragmentByTag(CreateFragment.TAG)).resultReturned(requestCode, resultCode, data);
}
CreateFragment:
private void chooseContact() {
Intent chooseContactInappIntent = new Intent( getActivity(), ContactPickerActivity.class );
getActivity().startActivityForResult( chooseContactInappIntent, PICK_CONTACT_REQUEST );
}
public void resultReturned( int requestCode, int resultCode, Intent data ) {
switch ( requestCode ) {
case PICK_CONTACT_REQUEST:
if ( resultCode == Activity.RESULT_OK ) {
//Do Cool Things
default:
break;
}
}
ContactPickerActivity:
#Override
public void onContactSelected( Object data) {
Intent returnIntent = new Intent();
returnIntent.putExtra("data",data);
setResult(RESULT_OK,returnIntent);
finish();
}
Activity One hosts a CreateFragment, when the use clicks a button in the CreateFragment the fragment calls the chooseContact() method which starts a new ActivityForResult. The ContactPickerActivity then displays the user a list of contacts. When the user selects a contact the onContactSelected method is called creating a new intent to pass back the selected data and then calls finish. I thought this would either call the onActivityResult in the base activity or the CreateFragment, but neither are called.
Any Ideas?
Thanks,
Nathan
In Fragment class work fine if you call at MainActivity onActivityResult override method and you don't need to changes inside the fragment class.
So
In Fragment that calls startActivityForResult like this:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && (requestCode == GALLERY_REQUEST || requestCode == CAMERA_REQUEST)) {
// its not complicated .....
}
}
In MainActivity Class are must to call onActivityResult Also.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
for (Fragment fragment : getSupportFragmentManager().getFragments()) {
fragment.onActivityResult(requestCode, resultCode, data);
}
}
I have a fragment activity that displays an Android V2 Map. Inside I also have a onActivityResult used to handle the intent Extras that needs to be passed from the calling activity
public class DisplayMap extends FragmentActivity implements LocationListener {
#Override
protected void onCreate(Bundle arg0) {
// TODO Auto-generated method stub
super.onCreate(arg0);
setContentView(R.layout.map);
}
public void onActivityResult(int requestCode, int resultCode, Intent intent){
super.onActivityResult(requestCode, resultCode, intent);
Log.v("TEST", "********************************************");
}
}
Here is the code form the activity that calls it.
Intent i = new Intent("com.example.DisplayMap");
setResult(RESULT_OK, i);
startActivityForResult(i, 2014);
But somehow the onActivityResult is not called inside.
Thanks in advance.
Dennis
onActivityResult() needs to be in the calling activity, it retrieves the result, as the name suggests.
To return a result from the called activity you'll need to use setResult() and finish that activity:
called activity:
Intent i = new Intent();
setResult(RESULT_OK, i);
finish();
with RESULT_OK being passed as the resultCode parameter to onActivityResult() and i as intent
calling activity:
Intent i = new Intent("com.example.DisplayMap");
startActivityForResult(i, REQUEST_CODE);
and to receive the result:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
if(requestCode == REQUEST_CODE){
if (resultCode == RESULT_OK){
// RESULT OK, take the returned extras from intent and use them
}
}
}
I want to visible a edit button on my previous activity by going back using back button. But when I am using
#Override
public void onClick(View arg0) {
// finish the current activity
finish();
}
on my current activity, it is going back to previous activity, but edit button doesn't become visible.
If I am using onResume on previous activity
#Override
protected void onResume() {
super.onResume();
btnEdit.setVisibility(View.VISIBLE);
}
then it's always visible, no matter if the activity is resuming or created for the first time.
I am new in Android development, please help me to solve this problem.
Ok,
When you start the Activity, instead of startActivity(intent) use startActivityForResult(intent, 1989) (The 1989 can be whatever int you want).
When you return from the new Activity, before calling finish() do the following:
Intent returnIntent = new Intent();
returnIntent.putExtra("resultBool", true);
//Null checks, not strictly neccescary
if (getParent() == null)
{
setResult(Activity.RESULT_OK, returnIntent);
}
else
{
getParent().setResult(Activity.RESULT_OK, returnIntent);
}
finish();
Then in your first Activity override onActivityResult() like so:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
// The int you initially used
if (requestCode == 1989)
{
if (resultCode == RESULT_OK)
{
boolean result = data.getBooleanExtra("resultBool", false)
if (result)
//Show the button now
}
}
}
You can make a boolean variable which can be set to true when you finish and make a check with this boolean in your onResume method
Override onRestart() method
#Override
protected void onRestart () {
super.onResume();
btnEdit.setVisibility(View.VISIBLE);
}
Or start Activity with
startActivityForResult (Intent intent, int requestCode)
method and override
#Override
protected void onActivityResult (int requestCode, int resultCode, Intent data){
super.onActivityResult (requestCode,resultCode,data);
btnEdit.setVisibility(View.VISIBLE);
}