Issue
I'm calling startActivityForResult and the other Activity starts correctly, but the method onActivityResult isn't being called, it just calls OnCreate
This is the code where I call startActivityForResult
val intent = Intent(this, MainActivity::class.java);
startActivityForResult(intent, 2);
And this is the method onActivityResult on the MainActivity that isn't being called. This is the code
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
System.out.println("ewe")
super.onActivityResult(requestCode, resultCode, data)
// check if the request code is same as what is passed here it is 2
if (requestCode == 2) {
//do the things u wanted
val repetitionTime = 10L;
alarm = getSystemService(ALARM_SERVICE) as AlarmManager
val intent = Intent(applicationContext, Pedometer::class.java)
val pi = PendingIntent.getActivity(applicationContext, 0, intent, 0)
val alarmInfo = AlarmManager.AlarmClockInfo(System.currentTimeMillis() + 5000, pi);
alarm!!.setAlarmClock(alarmInfo, pi);
}
}
I have debugged the code and the method is not called at all.
I tried looking for answers and tried:
-I'm calling startActivityForResult correctly and with a positive number.
-There isn't android:launchMode="singleInstance" on my manifest. Here's my manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.walker">
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.ACTIVITY_RECOGNITION" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Walker">
<activity android:name=".Pedometer"></activity>
<activity android:name=".MainActivity" android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="TaskPlanner" android:exported="true"> </receiver>
</application>
</manifest>
I tried looking on the internet for more answers and tried looking in the documentation but couldn't find anything. Any help super appreciated, thanks in advance to anyone who answers!
SOLUTION
Looks like I just wasn't understanding how startActivityForResult. It turns out that startActivityForResult starts an activity and then after the new started activity finishes the onActivityResultMethod is called. I was trying to create a new activity and pass an argument using it right on the creation, so of course it didn't work.
To pass an argument to a new activity I just used:
val intent = Intent(this, MainActivity::class.java);
intent.putExtra("result",2);
setResult(2,intent);
startActivity(intent);
finish();//finishing activity
And then to retrieve the value
val extras = intent.extras
if (extras != null) {
val value = extras.getString("result")
extras.putString("result", null);
}
Solution
Looks like I just wasn't understanding how startActivityForResult. It turns out that startActivityForResult starts an activity and then after the new started activity finishes the onActivityResultMethod is called. I was trying to create a new activity and pass an argument using it right on the creation, so of course it didn't work.
To pass an argument to a new activity I just used:
val intent = Intent(this, MainActivity::class.java);
intent.putExtra("result",2);
setResult(2,intent);
startActivity(intent);
finish();//finishing activity
And then to retrieve the value
val extras = intent.extras
if (extras != null) {
val value = extras.getString("result")
extras.putString("result", null);
}
Answered By - Layer891203
Answer Checked By - David Goodson (JavaFixing Volunteer)