How to use onActivityForResult from one activity in another activity
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I am currently adding more features to a github project for my personal use. I need to create a list of applications to be shown in a grid view by either:
1. Writing package name by myself (this functionality already existed) or
2. Selecting from a list of apps (what i want to achieve) - all functionality is implemented in ChooserActivity using ChooserAdapter.
What would solve my problem is to pass information to the MainActivity and call its onActivityResult method, since there is a lot of things i cannot copy paste it.
For the clickListener (of each grid view item) I created the OnItemClickListener interface in ChooserAdapter and overriden it in the ChooserActivity.
ChooserActivity:
class ChooserActivity : BaseActivity(), ChooserAdapter.OnItemClickListener {
private val backButton: FloatingActionButton by bindView(R.id.back_button_2)
override fun onItemClick(app: ApplicationInfo?) {
val intent = EditorActivity.IntentBuilder(context)
.build()
startActivityForResult(intent, 1)
}
public override fun onActivityResult(requestCode: Int, resultCode: Int, intent: Intent?) {
Toast.makeText(this, "onActivityResult called ...", Toast.LENGTH_SHORT).show()
// !!!!!!
MainActivity().onActivityResult(requestCode, resultCode, intent)
//How can I write something similar to this to solve my problem?
}
ChooserAdapter:
public class ChooserAdapter extends RecyclerView.Adapter<ChooserAdapter.ChooserHolder>{
private List<ApplicationInfo> listOfApps;
private Context mContext;
private PackageManager pm;
private OnItemClickListener listener;
public ChooserAdapter(@NonNull Context context, List<ApplicationInfo> list, OnItemClickListener l) {
this.listOfApps = list;
this.mContext = context;
this.pm = context.getPackageManager();
this.listener =
new ChooserAdapter.OnItemClickListener(){
@Override
public void onItemClick(ApplicationInfo app){
try{
Intent intent = (new EditorActivity.IntentBuilder(mContext)).build();
((ChooserActivity)mContext).startActivityForResult(intent, 2);
}catch (Exception e){
Toast.makeText(mContext, "Error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
};
}
public class ChooserHolder extends RecyclerView.ViewHolder{
//stuff not useful to be shown
}}
The result of the above code resulted in not using the wanted MainActivity().onActivityResult(...), but the ChooserActivity's method.
android-studio android-activity kotlin
add a comment |
I am currently adding more features to a github project for my personal use. I need to create a list of applications to be shown in a grid view by either:
1. Writing package name by myself (this functionality already existed) or
2. Selecting from a list of apps (what i want to achieve) - all functionality is implemented in ChooserActivity using ChooserAdapter.
What would solve my problem is to pass information to the MainActivity and call its onActivityResult method, since there is a lot of things i cannot copy paste it.
For the clickListener (of each grid view item) I created the OnItemClickListener interface in ChooserAdapter and overriden it in the ChooserActivity.
ChooserActivity:
class ChooserActivity : BaseActivity(), ChooserAdapter.OnItemClickListener {
private val backButton: FloatingActionButton by bindView(R.id.back_button_2)
override fun onItemClick(app: ApplicationInfo?) {
val intent = EditorActivity.IntentBuilder(context)
.build()
startActivityForResult(intent, 1)
}
public override fun onActivityResult(requestCode: Int, resultCode: Int, intent: Intent?) {
Toast.makeText(this, "onActivityResult called ...", Toast.LENGTH_SHORT).show()
// !!!!!!
MainActivity().onActivityResult(requestCode, resultCode, intent)
//How can I write something similar to this to solve my problem?
}
ChooserAdapter:
public class ChooserAdapter extends RecyclerView.Adapter<ChooserAdapter.ChooserHolder>{
private List<ApplicationInfo> listOfApps;
private Context mContext;
private PackageManager pm;
private OnItemClickListener listener;
public ChooserAdapter(@NonNull Context context, List<ApplicationInfo> list, OnItemClickListener l) {
this.listOfApps = list;
this.mContext = context;
this.pm = context.getPackageManager();
this.listener =
new ChooserAdapter.OnItemClickListener(){
@Override
public void onItemClick(ApplicationInfo app){
try{
Intent intent = (new EditorActivity.IntentBuilder(mContext)).build();
((ChooserActivity)mContext).startActivityForResult(intent, 2);
}catch (Exception e){
Toast.makeText(mContext, "Error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
};
}
public class ChooserHolder extends RecyclerView.ViewHolder{
//stuff not useful to be shown
}}
The result of the above code resulted in not using the wanted MainActivity().onActivityResult(...), but the ChooserActivity's method.
android-studio android-activity kotlin
Are you startChooserActivity
fromMainActivity
?
– Stanislav Bondar
Jan 3 at 15:00
@StanislavBondar Yes
– Ncs
Jan 3 at 15:12
add a comment |
I am currently adding more features to a github project for my personal use. I need to create a list of applications to be shown in a grid view by either:
1. Writing package name by myself (this functionality already existed) or
2. Selecting from a list of apps (what i want to achieve) - all functionality is implemented in ChooserActivity using ChooserAdapter.
What would solve my problem is to pass information to the MainActivity and call its onActivityResult method, since there is a lot of things i cannot copy paste it.
For the clickListener (of each grid view item) I created the OnItemClickListener interface in ChooserAdapter and overriden it in the ChooserActivity.
ChooserActivity:
class ChooserActivity : BaseActivity(), ChooserAdapter.OnItemClickListener {
private val backButton: FloatingActionButton by bindView(R.id.back_button_2)
override fun onItemClick(app: ApplicationInfo?) {
val intent = EditorActivity.IntentBuilder(context)
.build()
startActivityForResult(intent, 1)
}
public override fun onActivityResult(requestCode: Int, resultCode: Int, intent: Intent?) {
Toast.makeText(this, "onActivityResult called ...", Toast.LENGTH_SHORT).show()
// !!!!!!
MainActivity().onActivityResult(requestCode, resultCode, intent)
//How can I write something similar to this to solve my problem?
}
ChooserAdapter:
public class ChooserAdapter extends RecyclerView.Adapter<ChooserAdapter.ChooserHolder>{
private List<ApplicationInfo> listOfApps;
private Context mContext;
private PackageManager pm;
private OnItemClickListener listener;
public ChooserAdapter(@NonNull Context context, List<ApplicationInfo> list, OnItemClickListener l) {
this.listOfApps = list;
this.mContext = context;
this.pm = context.getPackageManager();
this.listener =
new ChooserAdapter.OnItemClickListener(){
@Override
public void onItemClick(ApplicationInfo app){
try{
Intent intent = (new EditorActivity.IntentBuilder(mContext)).build();
((ChooserActivity)mContext).startActivityForResult(intent, 2);
}catch (Exception e){
Toast.makeText(mContext, "Error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
};
}
public class ChooserHolder extends RecyclerView.ViewHolder{
//stuff not useful to be shown
}}
The result of the above code resulted in not using the wanted MainActivity().onActivityResult(...), but the ChooserActivity's method.
android-studio android-activity kotlin
I am currently adding more features to a github project for my personal use. I need to create a list of applications to be shown in a grid view by either:
1. Writing package name by myself (this functionality already existed) or
2. Selecting from a list of apps (what i want to achieve) - all functionality is implemented in ChooserActivity using ChooserAdapter.
What would solve my problem is to pass information to the MainActivity and call its onActivityResult method, since there is a lot of things i cannot copy paste it.
For the clickListener (of each grid view item) I created the OnItemClickListener interface in ChooserAdapter and overriden it in the ChooserActivity.
ChooserActivity:
class ChooserActivity : BaseActivity(), ChooserAdapter.OnItemClickListener {
private val backButton: FloatingActionButton by bindView(R.id.back_button_2)
override fun onItemClick(app: ApplicationInfo?) {
val intent = EditorActivity.IntentBuilder(context)
.build()
startActivityForResult(intent, 1)
}
public override fun onActivityResult(requestCode: Int, resultCode: Int, intent: Intent?) {
Toast.makeText(this, "onActivityResult called ...", Toast.LENGTH_SHORT).show()
// !!!!!!
MainActivity().onActivityResult(requestCode, resultCode, intent)
//How can I write something similar to this to solve my problem?
}
ChooserAdapter:
public class ChooserAdapter extends RecyclerView.Adapter<ChooserAdapter.ChooserHolder>{
private List<ApplicationInfo> listOfApps;
private Context mContext;
private PackageManager pm;
private OnItemClickListener listener;
public ChooserAdapter(@NonNull Context context, List<ApplicationInfo> list, OnItemClickListener l) {
this.listOfApps = list;
this.mContext = context;
this.pm = context.getPackageManager();
this.listener =
new ChooserAdapter.OnItemClickListener(){
@Override
public void onItemClick(ApplicationInfo app){
try{
Intent intent = (new EditorActivity.IntentBuilder(mContext)).build();
((ChooserActivity)mContext).startActivityForResult(intent, 2);
}catch (Exception e){
Toast.makeText(mContext, "Error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
};
}
public class ChooserHolder extends RecyclerView.ViewHolder{
//stuff not useful to be shown
}}
The result of the above code resulted in not using the wanted MainActivity().onActivityResult(...), but the ChooserActivity's method.
android-studio android-activity kotlin
android-studio android-activity kotlin
asked Jan 3 at 13:54
NcsNcs
257
257
Are you startChooserActivity
fromMainActivity
?
– Stanislav Bondar
Jan 3 at 15:00
@StanislavBondar Yes
– Ncs
Jan 3 at 15:12
add a comment |
Are you startChooserActivity
fromMainActivity
?
– Stanislav Bondar
Jan 3 at 15:00
@StanislavBondar Yes
– Ncs
Jan 3 at 15:12
Are you start
ChooserActivity
from MainActivity
?– Stanislav Bondar
Jan 3 at 15:00
Are you start
ChooserActivity
from MainActivity
?– Stanislav Bondar
Jan 3 at 15:00
@StanislavBondar Yes
– Ncs
Jan 3 at 15:12
@StanislavBondar Yes
– Ncs
Jan 3 at 15:12
add a comment |
1 Answer
1
active
oldest
votes
You have to start ChooserActivity
with startActivityForResult()
from MainActivity
and then pass result in this way EditorActivity
-> ChooserActivity
-> MainActivity
class ChooserActivity : BaseActivity(), ChooserAdapter.OnItemClickListener {
....
public override fun onActivityResult(requestCode: Int, resultCode: Int, intent: Intent?) {
Toast.makeText(this, "onActivityResult called ...", Toast.LENGTH_SHORT).show()
setResult(Activity.RESULT_OK, intent)
finish()
}
....
}
Override onActivityResult(...)
in MainActivity
and handle result
Didn't think of this simple and efficient way to call methods. It worked, thank you
– Ncs
Jan 3 at 16:34
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54023688%2fhow-to-use-onactivityforresult-from-one-activity-in-another-activity%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You have to start ChooserActivity
with startActivityForResult()
from MainActivity
and then pass result in this way EditorActivity
-> ChooserActivity
-> MainActivity
class ChooserActivity : BaseActivity(), ChooserAdapter.OnItemClickListener {
....
public override fun onActivityResult(requestCode: Int, resultCode: Int, intent: Intent?) {
Toast.makeText(this, "onActivityResult called ...", Toast.LENGTH_SHORT).show()
setResult(Activity.RESULT_OK, intent)
finish()
}
....
}
Override onActivityResult(...)
in MainActivity
and handle result
Didn't think of this simple and efficient way to call methods. It worked, thank you
– Ncs
Jan 3 at 16:34
add a comment |
You have to start ChooserActivity
with startActivityForResult()
from MainActivity
and then pass result in this way EditorActivity
-> ChooserActivity
-> MainActivity
class ChooserActivity : BaseActivity(), ChooserAdapter.OnItemClickListener {
....
public override fun onActivityResult(requestCode: Int, resultCode: Int, intent: Intent?) {
Toast.makeText(this, "onActivityResult called ...", Toast.LENGTH_SHORT).show()
setResult(Activity.RESULT_OK, intent)
finish()
}
....
}
Override onActivityResult(...)
in MainActivity
and handle result
Didn't think of this simple and efficient way to call methods. It worked, thank you
– Ncs
Jan 3 at 16:34
add a comment |
You have to start ChooserActivity
with startActivityForResult()
from MainActivity
and then pass result in this way EditorActivity
-> ChooserActivity
-> MainActivity
class ChooserActivity : BaseActivity(), ChooserAdapter.OnItemClickListener {
....
public override fun onActivityResult(requestCode: Int, resultCode: Int, intent: Intent?) {
Toast.makeText(this, "onActivityResult called ...", Toast.LENGTH_SHORT).show()
setResult(Activity.RESULT_OK, intent)
finish()
}
....
}
Override onActivityResult(...)
in MainActivity
and handle result
You have to start ChooserActivity
with startActivityForResult()
from MainActivity
and then pass result in this way EditorActivity
-> ChooserActivity
-> MainActivity
class ChooserActivity : BaseActivity(), ChooserAdapter.OnItemClickListener {
....
public override fun onActivityResult(requestCode: Int, resultCode: Int, intent: Intent?) {
Toast.makeText(this, "onActivityResult called ...", Toast.LENGTH_SHORT).show()
setResult(Activity.RESULT_OK, intent)
finish()
}
....
}
Override onActivityResult(...)
in MainActivity
and handle result
answered Jan 3 at 15:25
Stanislav BondarStanislav Bondar
3,53322034
3,53322034
Didn't think of this simple and efficient way to call methods. It worked, thank you
– Ncs
Jan 3 at 16:34
add a comment |
Didn't think of this simple and efficient way to call methods. It worked, thank you
– Ncs
Jan 3 at 16:34
Didn't think of this simple and efficient way to call methods. It worked, thank you
– Ncs
Jan 3 at 16:34
Didn't think of this simple and efficient way to call methods. It worked, thank you
– Ncs
Jan 3 at 16:34
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54023688%2fhow-to-use-onactivityforresult-from-one-activity-in-another-activity%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Are you start
ChooserActivity
fromMainActivity
?– Stanislav Bondar
Jan 3 at 15:00
@StanislavBondar Yes
– Ncs
Jan 3 at 15:12