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.
java android android-fragments
add a comment |
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.
java android android-fragments
do you want send data to fragment on comment activity'sonBackPressed
??
– 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
add a comment |
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.
java android android-fragments
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
java android android-fragments
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'sonBackPressed
??
– 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
add a comment |
do you want send data to fragment on comment activity'sonBackPressed
??
– 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
add a comment |
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);
}
}
}
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 inonActivityResult.
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 usestartActivityForResult
from Fragment or from adapter?
– Rumit Patel
2 days ago
yes.. Please check my questions for detail explanation
– user2269164
2 days ago
|
show 1 more comment
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);
}
}
}
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 inonActivityResult.
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 usestartActivityForResult
from Fragment or from adapter?
– Rumit Patel
2 days ago
yes.. Please check my questions for detail explanation
– user2269164
2 days ago
|
show 1 more comment
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);
}
}
}
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 inonActivityResult.
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 usestartActivityForResult
from Fragment or from adapter?
– Rumit Patel
2 days ago
yes.. Please check my questions for detail explanation
– user2269164
2 days ago
|
show 1 more comment
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);
}
}
}
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);
}
}
}
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 inonActivityResult.
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 usestartActivityForResult
from Fragment or from adapter?
– Rumit Patel
2 days ago
yes.. Please check my questions for detail explanation
– user2269164
2 days ago
|
show 1 more comment
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 inonActivityResult.
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 usestartActivityForResult
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
|
show 1 more comment
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%2f53411141%2factivity-to-fragment-sending-data-and-access-arraylist-in-fragment%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
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