onActivityResult called immediately on startActivityForResult - android

I am trying to get results from native activity in React Native Module. When I called startActivityForResult from my React Native Module, onActivityResult is called immediately even though the activity shows up but RESULT_CANCELED is immediately returned to onActivityResult().
I have followed the official guide from here
My Module code to start activity:
Activity currentActivity = getCurrentActivity();
if (currentActivity != null) {
Intent tgIntent = new Intent(currentActivity, SecondActivity.class);
tgIntent.setFlags(FLAG_ACTIVITY_NEW_TASK);
currentActivity.startActivityForResult(tgIntent, REQUEST_CODE_ENROLLMENT);
}
I tried both of following to set activity event listener but nothing changes
context.addActivityEventListener(activityEventListener);
context.addActivityEventListener(this);

Remove the "New Task" flag from the intent.
startActivityForResult must be under the same task as the calling activity.
According to documentation:
if the activity you are launching uses Intent#FLAG_ACTIVITY_NEW_TASK, it will not run in your task and thus you will immediately receive a cancel result.

Related

Open app with multiple inner Intents and wait for result

I´ve an app (APP1) which should open other app (APP2) and wait for the result. I´m doing it this way.
private fun startBridgeActivity(fileName: String, isProduction: Boolean) {
val intent = Intent(Intent.ACTION_MAIN)
when (isProduction) {
true -> {
intent.component = ComponentName(
"com.myapp",
"com.myapp.view.ui.ItemSelectionActivity"
)
}
else -> {
intent.component = ComponentName(
"com.myapp.dev",
"com.myapp.view.ui.ItemSelectionActivity"
)
}
}
try {
startActivityForResult(intent, REQUEST_CODE)
} catch (e: Exception) {
e.printStackTrace()
Toast.makeText(this#MainActivity, "This activity does not exist", Toast.LENGTH_LONG).show()
}
}
Then APP2 receives this intent and opens itself. The launch method on this APP2 it´s just the standard.
The navigation inside APP2 is as follows:
ReceiverActivity - Step1Activity - Step2Activity - LastActivity
I´ve tried setting the result and finishing LastActivity, but it doens't work. Also tried with finishAffinity and finishAndRemoveTasks but they also didn't work.
Then what I did was calling ReceiverActivity from LastActivity and setting there the result and finishing it. But that leaves me on the homescreen and the result wont get to APP1.
val receiverActivityIntent = Intent(this#LastActivity, ReceiverActivity::class.java)
receiverActivityIntent.putExtra("end", true)
TaskStackBuilder.create(this#ReceiverActivity)
.addParentStack(ReceiverActivity::class.java)
.addNextIntent(receiverActivityIntent)
.startActivities()
finish()
I readed some other SO answers where someone wrote that the Activity from APP1 would be added to the stack of APP2 and that might be why the app goes to the homescreen.
Any help will be appreciated.
Thanks.
Your app launches ItemSelectionActivity using startActivityForResult(). Your app will get the callback onActivityResult() when ItemSelectionActivity finishes. It will receive the results that ItemSelectionActivity sets when it calls setResult().
If ItemSelectionActivity needs to launch other activities before it can get the results to send back to your app, you can do this in a few possible ways:
Use FLAG_ACTIVITY_FORWARD_RESULT
When ItemSelectionActivity launches another Activity, it should set the flag Intent.FLAG_ACTIVITY_FORWARD_RESULT in the Intent and call startActvity() (do NOT call startActivityForResult(). The Activity that is being launched must then call setResult() with the results and that data will be passed back to your app. You are basically "forwarding" the request for a result from one Activity to the next. Since you seem to have several activities to go through before you get a result, you can continue to forward the responsibility from one Activity to the next Activity. The last one in the chain should then call setResult() and those results will be passed back to your app in onActivityResult().
Chain startActivityForResult() calls:
When ItemSelectionActivity launches another Activity, it should call startActvityForResult(). The Activity that is being launched must then call setResult() with the results and that data will be passed back to ItemSelefctionActivity in onActivityResult(). ItemSelectionActivity should then itself call setResult() with the data and finish(). The results will be passed back to your app. Since you seem to have several activities to go through before you get a result, you can continue to chain these calls so that each Activity launches the next Activity using starActivityForResult() and the called Activity needs to pass the results back usingsetResult()`.
Have LastActivity deliver the result to ItemSelectionActivity:
This is the solution that you have already tried to implement. LastActivity returns the result directly to ItemSelectionActivity. However, your implementation is broken. DO NOT USE TaskStackBuilder to accomplish this! TaskStackBuilder has a lot of side effects that destroy the Activity stack within the task. What you want to do instead is this:
val receiverActivityIntent = Intent(this#LastActivity, ReceiverActivity::class.java)
receiverActivityIntent.putExtra("end", true)
// add the results to the Intent
receiverActivityIntent.putExtra("results", results)
// Set the CLEAR_TOP and SINGLE_TOP flags (if necessary) to remove any
// activities that are on the stack between ReceiverActivity and LastActivity
receiverActivityIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP)
startActivity(receiverActivityIntent)
// Calling finish() here isn't necessary if you set the Intent flags
finish()
NOTE: My Kotlin syntax may not be 100% correct, but hopefully you get the idea.
With this solution, the results will be delivered to ReceiverActivity in onNewIntent(). You will need to override onNewIntent(), get the results from the passed Intent and then call setResult() with the results to pass them back to your app.

Android how to start activity without intent or backround

Suppose I have two activities A and B activity A which contains a button I want to start Activity B when I press Button without intent.
According the Oficial Documentation:
An intent is an abstract description of an operation to be performed. It can be used with startActivity to launch an Activity, broadcastIntent to send it to any interested BroadcastReceiver components, and startService(Intent) or bindService(Intent, ServiceConnection, int) to communicate with a background Service.
An Intent provides a facility for performing late runtime binding between the code in different applications. Its most significant use is in the launching of activities, where it can be thought of as the glue between activities. It is basically a passive data structure holding an abstract description of an action to be performed.
So you have to use it to open activities with no exceptions or workarounds, if you do that, you are ignoring the entire system architecture.
There is no way to start an activity from anotherone without an intent.
If the reason of not using Intent that you don't want the the user to re-enter the previous activity
You can use finish() to finish that activity intent after you done work with
if(currentUser == null){
startActivity(new Intent(MainActivity.this,StartActivity.class));
finish();
}
So user will be unable to back again
If you want to do some code while the activity is finishing
You can use onDestroy() override method, Sometimes it can also be called if the activity is being killed by the android itself so you can add
isFinishing() function
Inside onDestroy() method which checks whether the application is closing by the call finish() returning true or otherwise by anything else returning false then you can easily specify your code for each situation.
#Override
protected void onDestroy() {
super.onDestroy();
if(isFinishing()){
// Activity is being destroyed by the function `finish()`
// What to do...
}else{
// Activity is being destroyed anonymously without `finish()`
// What to do...
}
}
Put your activity inside a Fragment and start the fragment fromo the button.
These are the possible ways to start any Activity
1st
startActivity(new Intent(Activity_A.this, Activity_B.class));
2nd
Intent intent = new Intent(Activity_A.this, Activity_B.class);
startActivity(intent);
3rd
Intent intent = new Intent(Activity_A.this, Activity_B.class);
startActivityForResult(intent,code);

Android get result from Camera Intent in onNewIntent

I have an Activity with android:launchMode="singleInstance" . I want launch camera from this and then process result, but startActivityForResult doesn't work with singleInstance. So maybe it's possible to get result from camera using onNewIntent method? If it's possible, how can I implement this?
You might want to look at both singleTask and singleInstance.
singleTask:
The system creates a new task and instantiates the activity at the root of the new task. However, if an instance of the activity already exists in a separate task, the system routes the intent to the existing instance through a call to its onNewIntent() method, rather than creating a new instance. Only one instance of the activity can exist at a time.
Note: Although the activity starts in a new task, the Back button
still returns the user to the previous activity.
singleInstance:
Same as "singleTask", except that the system doesn't launch any other activities into the task holding the instance. The activity is always the single and only member of its task; any activities started by this one open in a separate task.
Note: That means, the activity with launch mode is always in a single
activity instance task. This is a very specialized mode and should
only be used in the applications that are implemented entirely as one
activity.
You could try something like this
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Uri uri = intent.getData();
if (uri != null && uri.toString().startsWith(CALLBACK_URL)) {
try {
//load image goes
} catch (Exception e) {
Log.e(TAG, e.getMessage(), e);
}
}
}

android launched activity exit callback function

I am launching the android setting activity, from an android service.
Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.android.settings");
startActivity(LaunchIntent);
I am searching, how I can detect if the setting activity is closed,As I need some callback method.
If there is a callback method to know the settings or any other app like browsers,if launched in this method to know if the launched activity is exit its own.
Since settings and browsers are general code we can't put broadcast code in these activities.
Use startActivityForResult to launch the settings activity like so:
Intent LaunchIntent =
getPackageManager().getLaunchIntentForPackage("com.android.settings");
startActivityForResult(LaunchIntent, 42);
Usually, you would use a specific request code as the second argument, but in this case, you have no control over what the settings Activity could return as a result, and you only want to know when it finishes, so you can essentially make up a request code. It must be greater than 0, however. The docs state this here:
requestCode If >= 0, this code will be returned in onActivityResult() when the activity exits.
Then, you can override the onActivityResult method to handle what happens when the settings activity closes:
#Override
protected void onActivityResult (int requestCode, int resultCode, Intent data){
// Do whatever you would like to do
}
If you had used a specific request code when you started the Activity, this is where you would check if the result code exists, but since we aren't expecting any real result, the result code will likely be equal to RESULT_CANCELLED, but that's okay since you at least know that the Activity was cancelled.

Launch main activity if activity stack is empty

I have one activity which can be launched from several other activites, along with url filter intents.
On this activity I use the home icon in the actionbar as a back button, bringing the user back to the previous activity (and not as a "home" action). For now I do this by calling the finish() function. This works fine when working from within the application.
However, if launching the activity by an url filter intent, I want the home icon to bring the user to the main activity. Obviously, calling finish() will just close the activity.
So my question is, is there a way to check whether my application stack is empty and then launch the main acivity if true? Or am I attacking this the wrong way?
If your app is launched via url intent filter and it creates its own task, then you can use
if (isTaskRoot()) {
// This activity is at root of task, so launch main activity
} else {
// This activity isn't at root of task, so just finish()
}
EDIT: Added another possible method
If your app is launched into an existing task when it is launched via URL intent filter, then you can do something like the following:
When you launch your activity from other activities in your application, add an EXTRA to the Intent like this:
Intent intent = new Intent(this, MyActivity.class);
intent.putExtra("internal", "true");
startActivity(intent);
When your activity gets launched it can then check for the presence or absence of the EXTRA in the Intent to determine whether it was launched internally or via URL intent-filter, like this:
Intent intent = getIntent();
if (intent.hasExtra("internal")) {
// Launched internally
} else {
// Launched via intent-filter
}

Categories

Resources