I have a message class
class Message
{
public String message, sender;
public Message (String msg, String from)
{
message = msg;
sender = from;
}
public String toString () { return sender+":"+message; }
}
I defined a table variable for use this class in main activity:
Hashtable<String, ArrayList<Message>> table = new Hashtable<String, ArrayList<Message>>();
I am adding data in main activity to message class with this code:
table.put("user01", new ArrayList<Message>());
table.get("user01").add(new Message("message","sender"));
And I want to use this table variable in another fragment.I am passing with this code to fragment.
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
ConversationFragment conv = new ConversationFragment();
fragmentTransaction.add(R.id.container, conv);
ConversationFragment frgObj=ConversationFragment.newInstance(table.get("user01"));
fragmentTransaction.replace(R.id.container, frgObj,"ConversationFragment");
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
And this is newInstance function in Conversation fragment
static int myData;
public static ConversationFragment newInstance(ArrayList<Message> _extractedMessages){
ConversationFragment fragment = new ConversationFragment();
myData=_extractedMessages.size();
return fragment;
}
But I am getting nullpointer exception,for _extractedMessages.size().
This code is working
System.out.println(_extractedMessages);
But this is not working
System.out.println(_extractedMessages.size());
Ps:I can use size function in main activity,I can't use only in conversation fragment.
How can I fix it ?
Typically you pass parameters to the fragment in the Bundle (and make sure the Message class implements Parcelable):
public static ConversationFragment newInstance(ArrayList<Message> _extractedMessages){
ConversationFragment fragment = new ConversationFragment();
Bundle bundle = new Bundle();
bundle.putParcelable(KEY_MESSAGES, _extractedMessages);
fragment.setArguments(bundle);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myData = getArguments().getParcelable(KEY_MESSAGES);
}
Related
I want to make a factory method in java class then call that method in activity to loading multiple fragments.
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.Men: {
FirstFragment firstfragment = new FirstFragment();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, firstfragment);
transaction.commit();
break;
case R.id.Men: {
SecondFragment secondfragment = new SecondFragment();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, secondfragment);
transaction.commit();
break;
}
}
static factory method, you can simply keep everything in fragment and no need to expose. Whoever needs to use this fragment will have a clear idea what it requires and also will not need to know what keys are. Just send the required params will be enough for them.
class FragmentFoo extends Fragment{
private static final String KEY_NAME = "name";
private String name;
public static Fragment newInstance(String name){
Bundle bundle = new Bundle();
bundle.putString(KEY_NAME, "name");
Fragment fragment = new FragmentFoo();
fragment.setArgs(bundle);
return fragment;
}
}
I am storing some values in array list now i want to pass the same array list object to fragment from an activity. So how can i send the array list object from an activity and receive the same object in fragment.
Try this code in your activity, It is a code snippet from my working application.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
yourArrayList.add("test");
yourArrayList.add("test2")
Bundle bundle = new Bundle();
bundle.putStringArrayList("arrayList", yourArrayList);
yourFragment yourFragment = new yourFragment();
yourFragment.setArguments(bundle);
fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.main_container, yourFragment);
fragmentTransaction.commit();
}
In your fragment you can access the value like this
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ArrayList<String> values = getArguments().getStringArrayList("arrayList");
}
pass activity should follow this lines
Bundle bundle = new Bundle();
bundle.putStringArrayList("valuesArray", namesArray);
namesFragment myFragment = new namesFragment();
myFragment.setArguments(bundle);
fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.main_container, myFragment);
fragmentTransaction.commit();
get Fragment inside follow this lines
ArrayList<ObjectName> arraylist = extras.getParcelableArrayList("valuesArray");
You can do this by following the usual way of Fragment-Activity communication. That is:
1) Declare a public interface within your Fragment class.
2) Inside this interface declare a getter method for your list.
3) Make your respective Activity implement this interface.
4) Inside your Fragment, do this:
#Override
public void onAttach(Context context) {
super.onAttach(context);
YourInterfaceType activity = (YourInterfaceType) context;
List<> yourList = activity.getMyList();
}
To make it more clear:
Your activity will be as follows:
public class MyActivity extends Activity implements YourInterfaceType{
#Override
public void getMyList(){
return yourList;
}
}
just put your array with setargument in Actiivty and get Array
getArgument in fragment.
Activity
Bundle bundle = new Bundle();
bundle.putStringArrayList("valuesArray", namesArray);
namesFragment myFragment = new namesFragment();
myFragment.setArguments(bundle);
fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.main_container, myFragment);
fragmentTransaction.commit();
fragment
ArrayList<Model> values = getArguments().getStringArrayList("valuesArray");
I was trying out android fragments, and i could not understand this; please help
public class MainActivity extends FragmentActivity {
private static final String TAG = "MyActivity";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (findViewById(R.id.fragment_container) !=null) {
if(savedInstanceState!=null){
return ;
}
ArticleFragment first_fragment = new ArticleFragment();
HeadlineFragment second_fragment = new HeadlineFragment();
first_fragment.setArguments(getIntent().getExtras());
second_fragment.setArguments(getIntent().getExtras());
FragmentTransaction manager=getSupportFragmentManager().beginTransaction();
manager.add(R.id.fragment_container, first_fragment,"first");
manager.add(R.id.yo, second_fragment,"second");
manager.commit();
}
}
now i want to remove second_fragment at a button press and replace it with another. So i tried this
if(getSupportFragmentManager().findFragmentByTag("second")!=null) {
Log.d(TAG,"found");
abc newFragment = new abc();
Bundle args = new Bundle();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.remove(getSupportFragmentManager().findFragmentByTag("second"));
transaction.add(R.id.fragment_container,newFragment,"third");
transaction.addToBackStack(null);
transaction.commit();
}
but when i press that button again and again, the if condition passes.
What is wrong here?
Don't Do removing and adding. Simply Replace second fragment with the new one. You will be done.
I have trouble understanding in FragmentTransaction.replace(id, fragment, 'string') what does .replace actually
I read somewhere that it will replace the content of view id with fragment but in my
public class MainActivity extends
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (findViewById(R.id.fragment_container) !=null) {
if(savedInstanceState!=null){
return ;
}
ArticleFragment first_fragment = new ArticleFragment();
HeadlineFragment second_fragment = new HeadlineFragment();
first_fragment.setArguments(getIntent().getExtras());
second_fragment.setArguments(getIntent().getExtras());
FragmentTransaction manager=getSupportFragmentManager().beginTransaction();
manager.add(R.id.fragment_container, first_fragment,"first");
manager.add(R.id.fragment_container, second_fragment,"second");
manager.commit();
}
abc newFragment = new abc();
Bundle args = new Bundle();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, newFragment,"second");
transaction.addToBackStack(null);
transaction.commit();
}
}
it only replaces the first fragment that i added to Transaction, i.e. first_fragment.
And how do i replace a specific fragment?
The issue seems to be with R.id.fragment_container. You should have a different first argument on the line below in order to replace a fragment that is not your "first" fragment:
manager.add(R.id.fragment_container, second_fragment,"second");
I'm trying to display a ListFragment that'll show user comments from within another Fragment. For testing purposes, I'm calling the ListFragment using an "onClick" method from within the parent Fragment. However, I can't seem to get the ListFragment to show, as it has no "show" method. Are there any obvious problems with my code that I can fix?
This is the method called when I click a button to show the ListFragment.
private void showComments(JSONArray comments) {
ListFragment newFragment = CommentsFragment.newInstance(comments);
}
And here is the ListFragment itself.
public class CommentsFragment extends ListFragment {
public static CommentsFragment newInstance(JSONArray passedComments) {
CommentsFragment f = new CommentsFragment();
ArrayList<String> adapter = convertJSON(passedComments);
Bundle args = new Bundle();
args.putStringArrayList("adapter", adapter);
f.setArguments(args);
return f;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Bundle args = new Bundle();
ArrayList<String> commentsArray = args.getStringArrayList("adapter");
ArrayAdapter<String> commentsAdapter = new ArrayAdapter<String>(
getActivity(), R.layout.comments_list,
commentsArray);
setListAdapter(commentsAdapter);
}
You have to commit the ListFragment with a FragmentManager, and also have a layout in the XML that will contain it.
Firstly add a FrameLayout to the XML where you want the ListFragment placed, and afterwards use getChildFragmentManager() to get a FragmentManager, that you can place the ListFragment with.
Try like this
private void showComments(JSONArray comments) {
ListFragment newFragment = CommentsFragment.newInstance(comments);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(android.R.id.content, newFragment);
fragmentTransaction.commit();
}