Reputation: 314
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.
Upvotes: 1
Views: 52
Reputation: 6245
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
Upvotes: 1