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 Nov 22 at 7:35









Shreya Prajapati

8211




8211










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
    Nov 22 at 7:53










  • but according to my answer you will be able to access your arrayList in onActivityResult. for sure.
    – Rumit Patel
    Nov 22 at 8:07










  • No I am not able to access it after finishing the activity.
    – user2269164
    Nov 22 at 9:35










  • do you use startActivityForResult from Fragment or from adapter?
    – Rumit Patel
    Nov 22 at 9:51










  • yes.. Please check my questions for detail explanation
    – user2269164
    Nov 22 at 10:15











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
    Nov 22 at 7:53










  • but according to my answer you will be able to access your arrayList in onActivityResult. for sure.
    – Rumit Patel
    Nov 22 at 8:07










  • No I am not able to access it after finishing the activity.
    – user2269164
    Nov 22 at 9:35










  • do you use startActivityForResult from Fragment or from adapter?
    – Rumit Patel
    Nov 22 at 9:51










  • yes.. Please check my questions for detail explanation
    – user2269164
    Nov 22 at 10:15















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
    Nov 22 at 7:53










  • but according to my answer you will be able to access your arrayList in onActivityResult. for sure.
    – Rumit Patel
    Nov 22 at 8:07










  • No I am not able to access it after finishing the activity.
    – user2269164
    Nov 22 at 9:35










  • do you use startActivityForResult from Fragment or from adapter?
    – Rumit Patel
    Nov 22 at 9:51










  • yes.. Please check my questions for detail explanation
    – user2269164
    Nov 22 at 10:15













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,37632133




1,37632133












  • 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
    Nov 22 at 7:53










  • but according to my answer you will be able to access your arrayList in onActivityResult. for sure.
    – Rumit Patel
    Nov 22 at 8:07










  • No I am not able to access it after finishing the activity.
    – user2269164
    Nov 22 at 9:35










  • do you use startActivityForResult from Fragment or from adapter?
    – Rumit Patel
    Nov 22 at 9:51










  • yes.. Please check my questions for detail explanation
    – user2269164
    Nov 22 at 10:15


















  • 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
    Nov 22 at 7:53










  • but according to my answer you will be able to access your arrayList in onActivityResult. for sure.
    – Rumit Patel
    Nov 22 at 8:07










  • No I am not able to access it after finishing the activity.
    – user2269164
    Nov 22 at 9:35










  • do you use startActivityForResult from Fragment or from adapter?
    – Rumit Patel
    Nov 22 at 9:51










  • yes.. Please check my questions for detail explanation
    – user2269164
    Nov 22 at 10:15
















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
Nov 22 at 7:53




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
Nov 22 at 7:53












but according to my answer you will be able to access your arrayList in onActivityResult. for sure.
– Rumit Patel
Nov 22 at 8:07




but according to my answer you will be able to access your arrayList in onActivityResult. for sure.
– Rumit Patel
Nov 22 at 8:07












No I am not able to access it after finishing the activity.
– user2269164
Nov 22 at 9:35




No I am not able to access it after finishing the activity.
– user2269164
Nov 22 at 9:35












do you use startActivityForResult from Fragment or from adapter?
– Rumit Patel
Nov 22 at 9:51




do you use startActivityForResult from Fragment or from adapter?
– Rumit Patel
Nov 22 at 9:51












yes.. Please check my questions for detail explanation
– user2269164
Nov 22 at 10:15




yes.. Please check my questions for detail explanation
– user2269164
Nov 22 at 10:15


















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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


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

Berounka

Sphinx de Gizeh

Different font size/position of beamer's navigation symbols template's content depending on regular/plain...