Saturday, October 8, 2011

[android-developers] call startActivityForResult from button

Here is another absolute newbie question.

I seem to have a fundamental misunderstanding of how Android works. My
problem is simple:
I need to call startActivityForResult from the OnClick handler of a
button on my main application. The result is needed by the
application.

My reasoning to solve this is that I need to call
startActivityForResult from the application instance which houses the
button, so that the application class handles the onActivityResult.
Problem is Context does not expose startActivityForResult, but
Activity does.
The code compiles but I get a runtime exception on the call to
startActivityForResult:
java.lang.ClassCastException: android.app.Application cannot be cast
to android.app.Activity

Here is, I believe, the relevant code:

manifest xml ----------------------

<activity android:name=".BrowseForFolderActivity"
android:label="Browse For Folder">
<intent-filter>
<action android:name="com.john.BROWSE_FOR_FOLDER" />
<category
android:name="android.intent.category.CATEGORY_ALTERNATIVE" />
</intent-filter>
</activity>

main activity OnCreate -------------------

final Button button = (Button) findViewById(R.id.btnfolder);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),
BrowseForFolderActivity.class);
intent.setAction("com.john.browseforfolder");
intent.addCategory("CATEGORY_ALTERNATIVE");
((Activity)
getApplicationContext()).startActivityForResult(intent,
BROWSE_FOR_FOLDER);
}
});

Thanks for any advice.

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate