Activity to Fragment sending data and access arraylist in Fragment











up vote
0
down vote

favorite












I have a fragment which is present in Bottom Navigation Activity. The Fragments contain the custom recyclerview. There is a comment button when i press it opens another activity for comments. Below is in the RecyclerView adapter.



    viewholder.commentlay.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
//commenttofragment.clear();
Intent comment = new Intent(fp, com.fooddoof.fuddict.comment.class);
int id = dusers.get(position).getId();
int comcount = dusers.get(viewholder.getAdapterPosition()).getCommentcount();
comment.putExtra("id",id);
comment.putExtra("ownerid",userid);
comment.putExtra("maincommentposition",position);
comment.putExtra("commentcountonposition", comcount);
fp.startActivityForResult(comment,1);

}
});


In Comment activity after doing some tasks I need to send some values to this fragment. So I Override the OnBackPressed method. I have created a method in Fragment to receive it.



@Override
public void onBackPressed()
{
Bundle args = new Bundle();
args.putInt("maincommentcount",maincommentcount);
args.putInt("maincommentposition", maincommentposition);
FolowersPost f = new FolowersPost();
f.getdatafromcomment(args);
finish();
}


I receive it like below in Fragment.



public void getdatafromcomment(Bundle args)
{
int count = args.getInt("maincommentcount");
int p=args.getInt("maincommentposition",999999999);
Log.e("Shiva","count--->"+count+"p--->"+p);

}


The Values are received but I need to access the arraylist in Fragement which is passed in Adapter for displaying the recyclerView. But I am not able to access it while I am coming back to fragment which is present in the method under OnCreateView. I tried with OnResume to access it but works for some time only. I have declared the Arraylist as global variable also.










share|improve this question
























  • do you want send data to fragment on comment activity's onBackPressed ??
    – Rumit Patel
    Nov 21 at 12:17










  • @RumitPatel - I am able to send data from comment activity to fragment but i want to update the adapter in Fragment using the value i sent from comment activity. For this is I need to access the arraylist which is present in the Fragment after data is received. I am not able to access arraylist once i finish the activity.
    – user2269164
    Nov 21 at 15:16

















up vote
0
down vote

favorite












I have a fragment which is present in Bottom Navigation Activity. The Fragments contain the custom recyclerview. There is a comment button when i press it opens another activity for comments. Below is in the RecyclerView adapter.



    viewholder.commentlay.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
//commenttofragment.clear();
Intent comment = new Intent(fp, com.fooddoof.fuddict.comment.class);
int id = dusers.get(position).getId();
int comcount = dusers.get(viewholder.getAdapterPosition()).getCommentcount();
comment.putExtra("id",id);
comment.putExtra("ownerid",userid);
comment.putExtra("maincommentposition",position);
comment.putExtra("commentcountonposition", comcount);
fp.startActivityForResult(comment,1);

}
});


In Comment activity after doing some tasks I need to send some values to this fragment. So I Override the OnBackPressed method. I have created a method in Fragment to receive it.



@Override
public void onBackPressed()
{
Bundle args = new Bundle();
args.putInt("maincommentcount",maincommentcount);
args.putInt("maincommentposition", maincommentposition);
FolowersPost f = new FolowersPost();
f.getdatafromcomment(args);
finish();
}


I receive it like below in Fragment.



public void getdatafromcomment(Bundle args)
{
int count = args.getInt("maincommentcount");
int p=args.getInt("maincommentposition",999999999);
Log.e("Shiva","count--->"+count+"p--->"+p);

}


The Values are received but I need to access the arraylist in Fragement which is passed in Adapter for displaying the recyclerView. But I am not able to access it while I am coming back to fragment which is present in the method under OnCreateView. I tried with OnResume to access it but works for some time only. I have declared the Arraylist as global variable also.










share|improve this question
























  • do you want send data to fragment on comment activity's onBackPressed ??
    – Rumit Patel
    Nov 21 at 12:17










  • @RumitPatel - I am able to send data from comment activity to fragment but i want to update the adapter in Fragment using the value i sent from comment activity. For this is I need to access the arraylist which is present in the Fragment after data is received. I am not able to access arraylist once i finish the activity.
    – user2269164
    Nov 21 at 15:16















up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have a fragment which is present in Bottom Navigation Activity. The Fragments contain the custom recyclerview. There is a comment button when i press it opens another activity for comments. Below is in the RecyclerView adapter.



    viewholder.commentlay.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
//commenttofragment.clear();
Intent comment = new Intent(fp, com.fooddoof.fuddict.comment.class);
int id = dusers.get(position).getId();
int comcount = dusers.get(viewholder.getAdapterPosition()).getCommentcount();
comment.putExtra("id",id);
comment.putExtra("ownerid",userid);
comment.putExtra("maincommentposition",position);
comment.putExtra("commentcountonposition", comcount);
fp.startActivityForResult(comment,1);

}
});


In Comment activity after doing some tasks I need to send some values to this fragment. So I Override the OnBackPressed method. I have created a method in Fragment to receive it.



@Override
public void onBackPressed()
{
Bundle args = new Bundle();
args.putInt("maincommentcount",maincommentcount);
args.putInt("maincommentposition", maincommentposition);
FolowersPost f = new FolowersPost();
f.getdatafromcomment(args);
finish();
}


I receive it like below in Fragment.



public void getdatafromcomment(Bundle args)
{
int count = args.getInt("maincommentcount");
int p=args.getInt("maincommentposition",999999999);
Log.e("Shiva","count--->"+count+"p--->"+p);

}


The Values are received but I need to access the arraylist in Fragement which is passed in Adapter for displaying the recyclerView. But I am not able to access it while I am coming back to fragment which is present in the method under OnCreateView. I tried with OnResume to access it but works for some time only. I have declared the Arraylist as global variable also.










share|improve this question















I have a fragment which is present in Bottom Navigation Activity. The Fragments contain the custom recyclerview. There is a comment button when i press it opens another activity for comments. Below is in the RecyclerView adapter.



    viewholder.commentlay.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
//commenttofragment.clear();
Intent comment = new Intent(fp, com.fooddoof.fuddict.comment.class);
int id = dusers.get(position).getId();
int comcount = dusers.get(viewholder.getAdapterPosition()).getCommentcount();
comment.putExtra("id",id);
comment.putExtra("ownerid",userid);
comment.putExtra("maincommentposition",position);
comment.putExtra("commentcountonposition", comcount);
fp.startActivityForResult(comment,1);

}
});


In Comment activity after doing some tasks I need to send some values to this fragment. So I Override the OnBackPressed method. I have created a method in Fragment to receive it.



@Override
public void onBackPressed()
{
Bundle args = new Bundle();
args.putInt("maincommentcount",maincommentcount);
args.putInt("maincommentposition", maincommentposition);
FolowersPost f = new FolowersPost();
f.getdatafromcomment(args);
finish();
}


I receive it like below in Fragment.



public void getdatafromcomment(Bundle args)
{
int count = args.getInt("maincommentcount");
int p=args.getInt("maincommentposition",999999999);
Log.e("Shiva","count--->"+count+"p--->"+p);

}


The Values are received but I need to access the arraylist in Fragement which is passed in Adapter for displaying the recyclerView. But I am not able to access it while I am coming back to fragment which is present in the method under OnCreateView. I tried with OnResume to access it but works for some time only. I have declared the Arraylist as global variable also.







java android android-fragments






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 2 days ago









Shreya Prajapati

8011




8011










asked Nov 21 at 11:29









user2269164

3001416




3001416












  • do you want send data to fragment on comment activity's onBackPressed ??
    – Rumit Patel
    Nov 21 at 12:17










  • @RumitPatel - I am able to send data from comment activity to fragment but i want to update the adapter in Fragment using the value i sent from comment activity. For this is I need to access the arraylist which is present in the Fragment after data is received. I am not able to access arraylist once i finish the activity.
    – user2269164
    Nov 21 at 15:16




















  • do you want send data to fragment on comment activity's onBackPressed ??
    – Rumit Patel
    Nov 21 at 12:17










  • @RumitPatel - I am able to send data from comment activity to fragment but i want to update the adapter in Fragment using the value i sent from comment activity. For this is I need to access the arraylist which is present in the Fragment after data is received. I am not able to access arraylist once i finish the activity.
    – user2269164
    Nov 21 at 15:16


















do you want send data to fragment on comment activity's onBackPressed ??
– Rumit Patel
Nov 21 at 12:17




do you want send data to fragment on comment activity's onBackPressed ??
– Rumit Patel
Nov 21 at 12:17












@RumitPatel - I am able to send data from comment activity to fragment but i want to update the adapter in Fragment using the value i sent from comment activity. For this is I need to access the arraylist which is present in the Fragment after data is received. I am not able to access arraylist once i finish the activity.
– user2269164
Nov 21 at 15:16






@RumitPatel - I am able to send data from comment activity to fragment but i want to update the adapter in Fragment using the value i sent from comment activity. For this is I need to access the arraylist which is present in the Fragment after data is received. I am not able to access arraylist once i finish the activity.
– user2269164
Nov 21 at 15:16














1 Answer
1






active

oldest

votes

















up vote
0
down vote













You are already using startActivityForResult. now you just need to use onActivityResult.



But you just need to start activity from fragment instead of from adapter.



onClick from fragment:



Intent comment = new Intent(getActivity(), com.fooddoof.fuddict.comment.class);
startActivityForResult(comment, 1);


onBackPressed in your comment activity:



    @Override
public void onBackPressed() {
Intent returnIntent = new Intent();
returnIntent.putExtra("maincommentcount",10);
returnIntent.putExtra("maincommentposition",20);
setResult(Activity.RESULT_OK,returnIntent);
finish();
// super.onBackPressed();
}


onActivityResult in fragment:



@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if (resultCode == Activity.RESULT_OK) {
int mMaincommentcount = data.getIntExtra("maincommentcount", 0);
int mMaincommentposition = data.getIntExtra("maincommentposition", 0);

System.out.println("mMaincommentcount = " + mMaincommentcount + ", mMaincommentposition = " + mMaincommentposition);
}
}
}





share|improve this answer





















  • I am getting the values without the above option. Problem what i faced is i am not able to access the arraylist inside the method.
    – user2269164
    2 days ago










  • but according to my answer you will be able to access your arrayList in onActivityResult. for sure.
    – Rumit Patel
    2 days ago










  • No I am not able to access it after finishing the activity.
    – user2269164
    2 days ago










  • do you use startActivityForResult from Fragment or from adapter?
    – Rumit Patel
    2 days ago










  • yes.. Please check my questions for detail explanation
    – user2269164
    2 days ago











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',
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%2f53411141%2factivity-to-fragment-sending-data-and-access-arraylist-in-fragment%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








up vote
0
down vote













You are already using startActivityForResult. now you just need to use onActivityResult.



But you just need to start activity from fragment instead of from adapter.



onClick from fragment:



Intent comment = new Intent(getActivity(), com.fooddoof.fuddict.comment.class);
startActivityForResult(comment, 1);


onBackPressed in your comment activity:



    @Override
public void onBackPressed() {
Intent returnIntent = new Intent();
returnIntent.putExtra("maincommentcount",10);
returnIntent.putExtra("maincommentposition",20);
setResult(Activity.RESULT_OK,returnIntent);
finish();
// super.onBackPressed();
}


onActivityResult in fragment:



@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if (resultCode == Activity.RESULT_OK) {
int mMaincommentcount = data.getIntExtra("maincommentcount", 0);
int mMaincommentposition = data.getIntExtra("maincommentposition", 0);

System.out.println("mMaincommentcount = " + mMaincommentcount + ", mMaincommentposition = " + mMaincommentposition);
}
}
}





share|improve this answer





















  • I am getting the values without the above option. Problem what i faced is i am not able to access the arraylist inside the method.
    – user2269164
    2 days ago










  • but according to my answer you will be able to access your arrayList in onActivityResult. for sure.
    – Rumit Patel
    2 days ago










  • No I am not able to access it after finishing the activity.
    – user2269164
    2 days ago










  • do you use startActivityForResult from Fragment or from adapter?
    – Rumit Patel
    2 days ago










  • yes.. Please check my questions for detail explanation
    – user2269164
    2 days ago















up vote
0
down vote













You are already using startActivityForResult. now you just need to use onActivityResult.



But you just need to start activity from fragment instead of from adapter.



onClick from fragment:



Intent comment = new Intent(getActivity(), com.fooddoof.fuddict.comment.class);
startActivityForResult(comment, 1);


onBackPressed in your comment activity:



    @Override
public void onBackPressed() {
Intent returnIntent = new Intent();
returnIntent.putExtra("maincommentcount",10);
returnIntent.putExtra("maincommentposition",20);
setResult(Activity.RESULT_OK,returnIntent);
finish();
// super.onBackPressed();
}


onActivityResult in fragment:



@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if (resultCode == Activity.RESULT_OK) {
int mMaincommentcount = data.getIntExtra("maincommentcount", 0);
int mMaincommentposition = data.getIntExtra("maincommentposition", 0);

System.out.println("mMaincommentcount = " + mMaincommentcount + ", mMaincommentposition = " + mMaincommentposition);
}
}
}





share|improve this answer





















  • I am getting the values without the above option. Problem what i faced is i am not able to access the arraylist inside the method.
    – user2269164
    2 days ago










  • but according to my answer you will be able to access your arrayList in onActivityResult. for sure.
    – Rumit Patel
    2 days ago










  • No I am not able to access it after finishing the activity.
    – user2269164
    2 days ago










  • do you use startActivityForResult from Fragment or from adapter?
    – Rumit Patel
    2 days ago










  • yes.. Please check my questions for detail explanation
    – user2269164
    2 days ago













up vote
0
down vote










up vote
0
down vote









You are already using startActivityForResult. now you just need to use onActivityResult.



But you just need to start activity from fragment instead of from adapter.



onClick from fragment:



Intent comment = new Intent(getActivity(), com.fooddoof.fuddict.comment.class);
startActivityForResult(comment, 1);


onBackPressed in your comment activity:



    @Override
public void onBackPressed() {
Intent returnIntent = new Intent();
returnIntent.putExtra("maincommentcount",10);
returnIntent.putExtra("maincommentposition",20);
setResult(Activity.RESULT_OK,returnIntent);
finish();
// super.onBackPressed();
}


onActivityResult in fragment:



@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if (resultCode == Activity.RESULT_OK) {
int mMaincommentcount = data.getIntExtra("maincommentcount", 0);
int mMaincommentposition = data.getIntExtra("maincommentposition", 0);

System.out.println("mMaincommentcount = " + mMaincommentcount + ", mMaincommentposition = " + mMaincommentposition);
}
}
}





share|improve this answer












You are already using startActivityForResult. now you just need to use onActivityResult.



But you just need to start activity from fragment instead of from adapter.



onClick from fragment:



Intent comment = new Intent(getActivity(), com.fooddoof.fuddict.comment.class);
startActivityForResult(comment, 1);


onBackPressed in your comment activity:



    @Override
public void onBackPressed() {
Intent returnIntent = new Intent();
returnIntent.putExtra("maincommentcount",10);
returnIntent.putExtra("maincommentposition",20);
setResult(Activity.RESULT_OK,returnIntent);
finish();
// super.onBackPressed();
}


onActivityResult in fragment:



@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if (resultCode == Activity.RESULT_OK) {
int mMaincommentcount = data.getIntExtra("maincommentcount", 0);
int mMaincommentposition = data.getIntExtra("maincommentposition", 0);

System.out.println("mMaincommentcount = " + mMaincommentcount + ", mMaincommentposition = " + mMaincommentposition);
}
}
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 at 6:48









Rumit Patel

1,29432133




1,29432133












  • I am getting the values without the above option. Problem what i faced is i am not able to access the arraylist inside the method.
    – user2269164
    2 days ago










  • but according to my answer you will be able to access your arrayList in onActivityResult. for sure.
    – Rumit Patel
    2 days ago










  • No I am not able to access it after finishing the activity.
    – user2269164
    2 days ago










  • do you use startActivityForResult from Fragment or from adapter?
    – Rumit Patel
    2 days ago










  • yes.. Please check my questions for detail explanation
    – user2269164
    2 days ago


















  • I am getting the values without the above option. Problem what i faced is i am not able to access the arraylist inside the method.
    – user2269164
    2 days ago










  • but according to my answer you will be able to access your arrayList in onActivityResult. for sure.
    – Rumit Patel
    2 days ago










  • No I am not able to access it after finishing the activity.
    – user2269164
    2 days ago










  • do you use startActivityForResult from Fragment or from adapter?
    – Rumit Patel
    2 days ago










  • yes.. Please check my questions for detail explanation
    – user2269164
    2 days ago
















I am getting the values without the above option. Problem what i faced is i am not able to access the arraylist inside the method.
– user2269164
2 days ago




I am getting the values without the above option. Problem what i faced is i am not able to access the arraylist inside the method.
– user2269164
2 days ago












but according to my answer you will be able to access your arrayList in onActivityResult. for sure.
– Rumit Patel
2 days ago




but according to my answer you will be able to access your arrayList in onActivityResult. for sure.
– Rumit Patel
2 days ago












No I am not able to access it after finishing the activity.
– user2269164
2 days ago




No I am not able to access it after finishing the activity.
– user2269164
2 days ago












do you use startActivityForResult from Fragment or from adapter?
– Rumit Patel
2 days ago




do you use startActivityForResult from Fragment or from adapter?
– Rumit Patel
2 days ago












yes.. Please check my questions for detail explanation
– user2269164
2 days ago




yes.. Please check my questions for detail explanation
– user2269164
2 days ago


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53411141%2factivity-to-fragment-sending-data-and-access-arraylist-in-fragment%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

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)