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;
}







0















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.










share|improve this question























  • Are you start ChooserActivity from MainActivity?

    – Stanislav Bondar
    Jan 3 at 15:00











  • @StanislavBondar Yes

    – Ncs
    Jan 3 at 15:12


















0















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.










share|improve this question























  • Are you start ChooserActivity from MainActivity?

    – Stanislav Bondar
    Jan 3 at 15:00











  • @StanislavBondar Yes

    – Ncs
    Jan 3 at 15:12














0












0








0








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.










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 3 at 13:54









NcsNcs

257




257













  • Are you start ChooserActivity from MainActivity?

    – 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











  • @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












1 Answer
1






active

oldest

votes


















1














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






share|improve this answer
























  • Didn't think of this simple and efficient way to call methods. It worked, thank you

    – Ncs
    Jan 3 at 16:34












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
});


}
});














draft saved

draft discarded


















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









1














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






share|improve this answer
























  • Didn't think of this simple and efficient way to call methods. It worked, thank you

    – Ncs
    Jan 3 at 16:34
















1














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






share|improve this answer
























  • Didn't think of this simple and efficient way to call methods. It worked, thank you

    – Ncs
    Jan 3 at 16:34














1












1








1







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






share|improve this answer













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







share|improve this answer












share|improve this answer



share|improve this answer










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



















  • 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




















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

'app-layout' is not a known element: how to share Component with different Modules

android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

WPF add header to Image with URL pettitions [duplicate]