Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BaseFragmentActivityApi16.startActivityForResult(intent, int, Bundle) throwing error when targeting Android O

Tags:

android

I started targeting android O in my project I get an error when calling startActivityForResult(intent, int, Bundle) with the error saying it can only be called from group id com.android.support.

Gradle:

compile 'com.android.support:design:26.0.0'
compile 'com.android.support:cardview-v7:26.0.0'
compile 'com.android.support:support-v13:26.0.0'
like image 708
Nick Mowen Avatar asked Jul 25 '17 15:07

Nick Mowen


People also ask

How to use the startactivityforresult method in Android?

In the below example, we are demonstrating the usage of the android startActivityForResult method. In the activity_main.xml file, we will drag a textview and a button from the palette. In the MainActivity.java file, we will write the code to invoke another activity and to retrieve the result from that activity.

Is startactivityforresult deprecated in Salesforce?

StartActivityForResult is deprecated !! to 1.2.0. It has deprecated startActivityForResult in favour of registerForActivityResult. communicating between two components. API design was simple enough to get started quickly but had its cons, like how Let’s try to understand how to use the new API with a few examples.

When should the onactivityresult method be invoked?

The onActivityResult method thus needs to be overridden when the second activity returns the result, i.e, it needs to be invoked automatically when the second activity returns the result. public void startActivityForResult (Intent intent, int requestCode)


2 Answers

It's a bit late but I've found a workaround. I tried ActivityCompat.startActivityForResult(Activity, intent, int, Bundle); and the warning is gone!

like image 56
islamdidarmd Avatar answered Oct 05 '22 11:10

islamdidarmd


Edit:

As per this link, this is a bug. For a workaround, Add this comment above the line of code which gives the warning:

//noinspection RestrictedApi

Old Ans:

I hope you are not importing wrong library. Fragment support library supports fragment for devices running versions prior to Android3.0.

As per this post in SO

Also remember to use Activity if you are using android.app.Fragment; use FragmentActivity if you are using android.support.v4.app.Fragment. Never attach a android.support.v4.app.Fragment to an android.app.Activity, as this will cause an exception to be thrown.

android.app.Fragment is different than android.support.v4.app.Fragment.

The support library one is annotated @RestrictTo(LIBRARY_GROUP), and also @hide - it's not meant to be a public API.

like image 44
Zohra Khan Avatar answered Oct 05 '22 10:10

Zohra Khan