null pointer when try to add fragment inside another fragment - android

I have the following structure
Activity
|________ Spinner
|________ TabHost
|_______ Fragment1
|________ LinearLayout
|_______ Fragment2
when I change the option in spinner i should add (Fragment3 OR Fragment4 OR Fragment5) inside LinearLayout
the code that I am using in the spinner
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
Fragment1 fragment = (BaseFragment) mTabFragments.get(mTabHost.getCurrentTab());
fragment.updateCurrentFragment(position);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
The code of adding the fragment
public void updateCurrentFragment (int index) {
switch (index) {
case 0:
replaceFragment(new Fragment3());
break;
case 1:
replaceFragment(new Fragment4());
break;
case 2:
replaceFragment(new Fragment5());
break;
}
}
public void replaceFragment (Fragment newFragement) {
FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
transaction.add(R.id.fragment_layout, newFragement);
transaction.setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
transaction.commit();
}
I am now getting null pointer exception, and getActivity = null

Though you haven't share the entire class, seems updateCurrentFragment() and replaceFragment() functions has been written within the Interface BaseFragment or within the individual Fragment1, Fragment2 etc clases, please correct me if I am wrong. On that case getActivity() will always return null as the new Fragment is yet to be added within the current activity. So may be you need to pass though the instance of the current activity (parent activity) to the BaseFragment or Individual claees extends BaseFragment and then use that instance for getActivity like
(instance of BaseFragment).getActivity() rather than only getActivity().
Here is your modified version of code
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
Fragment1 fragment = (BaseFragment) mTabFragments.get(mTabHost.getCurrentTab());
fragment.updateCurrentFragment(**this**,position);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
public void updateCurrentFragment (ParentActivity pAct,int index) {
switch (index) {
case 0:
replaceFragment(**pAct**,new Fragment3());
break;
case 1:
replaceFragment(**pAct**,new Fragment4());
break;
case 2:
replaceFragment(**pAct**,new Fragment5());
break;
}
}
public void replaceFragment ((ParentActivity pAct,Fragment newFragement) {
FragmentTransaction transaction = **pAct**.getActivity().getSupportFragmentManager().beginTransaction();
transaction.add(R.id.fragment_layout, newFragement);
transaction.setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
transaction.commit();
}
Let me know if this solves your problem.

Related

How to refreshing a fragment on MenuitemSelected of its activity?

I want to refresh the content of the fragment when a user clicks the refresher icon which is in menu action bar.
My application has three fragments on one activity with view pager; I tried to refresh all of them by calling them in onOptionsItemSelected() and I performed transactions to them, the application crashes when a user clicks refresh menu.
I read this question, it is likely similar to mine, but I couldn't find an appropriate answer to settle my problem: android: menu item click event from fragment I read this article too: but nothing helped me: https://developer.android.com/guide/topics/ui/menus maybe I am not doing it in a right way.
My code of refreshing all three fragments in the activity are here below:
#Override
public boolean onOptionsItemSelected(MenuItem item){
Fragment sentMsg=getSupportFragmentManager().findFragmentByTag("fragmentSentMsg");
Fragment receivedMsg=getSupportFragmentManager().findFragmentByTag("fragmentReceivedMsg");
Fragment allMsg=getSupportFragmentManager().findFragmentByTag("fragmentAllMsg");
FragmentTransaction fragmentTransaction=getSupportFragmentManager().beginTransaction();
switch (item.getItemId()){
case R.id.refresher_id:
fragmentTransaction.detach(sentMsg).attach(sentMsg).commit();
fragmentTransaction.detach(receivedMsg).attach(receivedMsg).commit();
fragmentTransaction.detach(allMsg).attach(allMsg).commit();
break;
}
return super.onOptionsItemSelected(item);
}
These are the code of a one fragment:
public class Page2_sent_msg extends Fragment {
//default constructor
public Page2_sent_msg(){}
#SuppressLint("ResourceType")
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState){
final View Page2_sent_msg=inflater.inflate(R.layout.page2_sent_msg,container,false);
ListView sentMsgListView=(ListView)Page2_sent_msg.findViewById(R.id.sentMsgListview);
ArrayList<String> sentMsgArrayList=new ArrayList<String>();
SQLite_database_helper_class myDb=new SQLite_database_helper_class(getContext());
Cursor result=myDb.getting_sms_from_db();
if (result.moveToFirst()){
do {
if (!result.getString(3).equals("Sent message")){
continue;
}else{
sentMsgArrayList.add("SMS No : "+result.getString(0)+"\n"
+"Address : "+result.getString(1)+"\n"
+"Date : "+result.getString(2)+"\n"
+"Type : "+result.getString(3)+"\n"
+"Content : "+"\n________\n\n"+result.getString(4)+"\n");
}
}while (result.moveToNext());
}
ArrayAdapter<String>sentMsgAdapter=new ArrayAdapter<>(getContext(),android.R.layout.simple_list_item_1,sentMsgArrayList);
sentMsgListView.setAdapter(sentMsgAdapter);
sentMsgListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//this is what will happen when a user clicks one item from the lis view
}
});
Page2_sent_msg.setTag("sentMsg");
return Page2_sent_msg;
}
I do really need a help. Kind regards!
Just write the fragment transaction in onclick of refresh button
fragment = new HomeFragment();
onFrangmentChange(fragment, true, false);
onFrangmentChange function will be like this
private void onFrangmentChange(BaseFragment fragment, boolean replace,
boolean addBackstack) {
this.fragment = fragment;
fm = getFragmentManager();
ft = fm.beginTransaction();
if (replace) {
ft.replace(R.id.fragment, fragment);
} else {
ft.add(R.id.fragment, fragment);
}
if (addBackstack) {
ft.addToBackStack(fragment.getClass().getSimpleName());
}
ft.commit();
}
Note:BaseFragment is nothing but A fragment which extend Fragment.You can write the common functions like checking network connection,email validation etc.in this fragment.
Add a method refresh in your fragment like this
public void refreshFragment() {
sentMsgArrayList.clear();
sentMsgAdapter.notifyDataSetChanged();
SQLite_database_helper_class myDb=new SQLite_database_helper_class(getContext());
Cursor result=myDb.getting_sms_from_db();
if (result.moveToFirst()){
do {
if (!result.getString(3).equals("Sent message")){
continue;
}else{
sentMsgArrayList.add("SMS No : "+result.getString(0)+"\n"
+"Address : "+result.getString(1)+"\n"
+"Date : "+result.getString(2)+"\n"
+"Type : "+result.getString(3)+"\n"
+"Content : "+"\n________\n\n"+result.getString(4)+"\n");
}
}while (result.moveToNext());
sentMsgAdapter.notifyDataSetChanged
}
Then called it once the user taps on refresh
#Override
public boolean onOptionsItemSelected(MenuItem item){
Fragment sentMsg=getSupportFragmentManager().findFragmentByTag("fragmentSentMsg");
Fragment receivedMsg=getSupportFragmentManager().findFragmentByTag("fragmentReceivedMsg");
Fragment allMsg=getSupportFragmentManager().findFragmentByTag("fragmentAllMsg");
switch (item.getItemId()){
case R.id.refresher_id:
sentMsg.refreshFragment();
receivedMsg.refreshFragment();
allMsg.refreshFragment();
break;
}
return super.onOptionsItemSelected(item);
}

Trying to make a flexible UI

I'am trying to make a app with a flexible UI.
I already implemented for handset devices (I have one activity and multiple fragments), and what I done was: The main fragment is a dashboard, and when I click in one button of it, he dashboard is replaced by a new fragment ( the clicked feature). Here is the code:
Dashboard fragment:
public class DashboardFragment extends Fragment {
GridView gridView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.activity_dashboard, container, false);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
gridView=(GridView)getView().findViewById(R.id.dashboard_grid);
gridView.setAdapter(new ImageAdapter(getActivity()));
gridView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
FragmentTransaction transaction = getFragmentManager().beginTransaction();
Fragment fragment = null ;
switch (position) {
case 0:
fragment = new TestFragment();
break;
case 1 :
fragment = new TestFragment();
break;
case 2 :
fragment = new TestFragment();
break;
case 3 :
fragment = new TestFragment();
break;
case 4 :
fragment = new TestFragment();
break;
}
transaction.replace(R.id.container, fragment);
transaction.addToBackStack(null);
transaction.commit();
}
});
}
}
and my Main Activity:
public class MainActivity extends FragmentActivity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (findViewById(R.id.container) != null) {
if (savedInstanceState != null) {
return;
}
// Create an instance of ExampleFragment
DashboardFragment firstFragment = new DashboardFragment();
firstFragment.setArguments(getIntent().getExtras());
getSupportFragmentManager().beginTransaction().add(R.id.container, firstFragment).commit();
}
}
}
Now, what I want is to adapt this code and use a layout for tablets, with the dashboard on the left and the choosen fragment on the right, like this:
What could I do? I already tried to adapt this example, but I can't because they only update the fragment, they don't replace it.
Check this great article about multi-pane development.
It also includes an example (Sections 10 and 11)
Basically you can check whether there is a fragment element for your "Fragment B" in the current layout. If yes, you just update its content, if no, then start an activity which has it in its layout, or replace one in the current layout.
DetailFragment fragment = (DetailFragment) getFragmentManager().findFragmentById(R.id.detail_frag);
if (fragment==null || ! fragment.isInLayout()) {
// start new Activity or replace
}
else {
fragment.update(...);
}

Added Fragment is not removed and causing App crash

Adding fragment works ok, but removing it crashes app. I thought removing will be straight forward but Im still a beginner so I must missing something. Can you take a look?
public class MainActivity extends Activity implements OnClickListener {
Button addFragment, closeFragment;
android.app.FragmentManager fragmentManager = getFragmentManager();
android.app.FragmentTransaction transaction = fragmentManager
.beginTransaction();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initialize();
}
private void initialize() {
addFragment = (Button) findViewById(R.id.bAddF);
closeFragment = (Button) findViewById(R.id.bCloseF);
addFragment
.setOnClickListener((android.view.View.OnClickListener) this);
closeFragment
.setOnClickListener((android.view.View.OnClickListener) this);
}
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//Instantiate fragment from ExampleFragment class
ExampleFragment fragment = new ExampleFragment();
switch(arg0.getId()){
case R.id.bAddF:
transaction.add(R.id.fragment_holder, fragment).commit();
break;
case R.id.bCloseF:
transaction.remove(fragment).commit();
break;
}
}
}
Your app crashes because you are saving the FragmentTransaction as a member variable. But FragmentTransactions cannot be reused. Furthermore, you are creating a new Fragment everytime, which means that the currently added Fragment will not be removed when clicking the remove Button.
Do it this way:
private ExampleFragment mFragment;
#Override
public void onClick(View v) {
//Instantiate fragment from ExampleFragment class
if(mFragment == null) mFragment = new ExampleFragment();
switch(v.getId()){
case R.id.bAddF:
getFragmentManager().beginTransaction().add(R.id.fragment_holder, mFragment, "tag").commit();
break;
case R.id.bCloseF:
getFragmentManager().beginTransaction().remove(mFragment).commit();
break;
}
}
Create a new FragmentTransaction for each add() or remove() procedure.
As an alternative for keeping your ExampleFragment as a member variable, you can use FragmentManager.findFragmentByTag("tag") to get the currently added fragment.
Alternative way:
#Override
public void onClick(View v) {
switch(v.getId()){
case R.id.bAddF:
getFragmentManager().beginTransaction().add(R.id.fragment_holder, new ExampleFragment(), "tag").commit();
break;
case R.id.bCloseF:
Fragment frag = getFragmentManager().findFragmentByTag("tag");
getFragmentManager().beginTransaction().remove(frag).commit();
break;
}
}

Android: Replacing a fragment in a view pager

I have two fragments SearchFragment and CreateFragment in a view pager inside a activity called TicketManagementActivity. Now when the user presses the search button in SearchFragment, I want SearchFragment to be replaced with SearchResultFragment. I should then be able to swipe between SeachResultFragment and CreateFragment in the ViewPager. Also when I press back from SearchResultFragment I should go back to SearchFragment.
Right now, when I press the button I get a blank screen instead of the layout of SearchResultFragment. When I press back I get to SearchFragment but now I have to click the button twice for the blank screen to come. Now after the blank screen comes after the double click, whenever I swipe to CreateFragment tab I get a blank screen instead of CreateFragment layout.
I looked at quite a number of questions on SO but none of them seem to be working for me. Most useful seems to be the first two answers in this question, but the first answer doesn't handle the back press, nor am I able to implement it. The second answer seems very implementable but I get errors which I have mentioned below.
My main TicketManagemementActivity:
public class TicketManagementActivity extends FragmentActivity implements
ActionBar.TabListener {
ViewPager viewPager;
TabsPagerAdapter adapter;
ActionBar actionBar;
String[] tabs={"Search", "Create"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ticket_management);
viewPager=(ViewPager)findViewById(R.id.pager);
actionBar=getActionBar();
adapter=new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(adapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
for(String tab_name : tabs){
actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this));
}
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
//removed methods for menu creation and filling and placeholder fragment for brevity on SO
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
}
My activity_ticket_management.xml which is layout set in onCreate of ticket management activity, just contains the viewpager
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
My TabsPagerAdapter class extending FragmentPagerAdapter:
public class TabsPagerAdapter extends FragmentPagerAdapter {
public TabsPagerAdapter(android.support.v4.app.FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int index) {
switch (index) {
case 0:
// Top Rated fragment activity
return new SearchFragment();
case 1:
// Games fragment activity
return new CreateFragment();
}
return null;
}
#Override
public int getCount() {
// get item count - equal to number of tabs
return 2;
}
}
Relevant part of my SearchFragment:
public class SearchFragment extends Fragment implements View.OnClickListener {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_search, container, false);
.
.//some widget initializations
.
return rootView;
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.ticket_search_btn: searchSigmaTickets();
break;
}
}
public void searchSigmaTickets(){
.
.
.
.//some operations
.
new SearchAsyncTask().execute();
}
}
private class SearchAsyncTask extends AsyncTask<Void, Void, Void>{
protected Void doInBackground(Void... params){
.
.//some more operation
.
}
protected void onPostExecute(Void param){
Fragment newFragment = new SearchResultFragment();
//Here I use getFragmentManager and not getChildFragmentManager
FragmentTransaction transaction = getFragmentManager().beginTransaction();
//HERE I try to replace the fragment. I'm not sure what id to pass, I pass the id of the main veiwpager in ticketmanagement activity
transaction.replace(R.id.pager, newFragment);
transaction.addToBackStack(null);
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
transaction.commit();
}
}
}
If I use getChildFragmentManager instead of getFragmentManager as mentioned in the second answer I get
06-25 06:55:32.045: E/AndroidRuntime(2797): java.lang.IllegalArgumentException: No view found for id 0x7f06003c (com.amberroad.sigmaticket:id/pager) for fragment SearchResultFragment{b2fed358 #0 id=0x7f06003c}
Sorry for the lengthy question, how should I solve this?
Kartik, get ready for a lengthy answer to your lenghty question. Replacing fragments in a viewpager is quite involved but is very possible and can look super slick. First, you need to let the viewpager itself handle the removing and adding of the fragments. What is happening is when you replace the fragment inside of SearchFragment, your viewpager retains its fragment views. So you end up with a blank page because the SearchFragment gets removed when you try to replace it.
The solution is to create a listener inside of your viewpager that will handle changes made outside of it so first add this code to the bottom of your adapter.
public interface nextFragmentListener {
public void fragment0Changed(String newFragmentIdentification);
}
Then you need to create a private class in your viewpager that becomes a listener for when you want to change your fragment. For example you could add something like this. Notice that it implements the interface that was just created. So whenever you call this method, it will run the code inside of the class below.
private final class fragmentChangeListener implements nextFragmentListener {
#Override
public void fragment0Changed(String fragment) {
//I will explain the purpose of fragment0 in a moment
fragment0 = fragment;
manager.beginTransaction().remove(fragAt0).commit();
switch (fragment){
case "searchFragment":
fragAt0 = SearchFragment.newInstance(listener);
break;
case "searchResultFragment":
fragAt0 = Fragment_Table.newInstance(listener);
break;
}
notifyDataSetChanged();
}
There are two main things to point out here: 1)fragAt0 is a "flexible" fragment. It can take on whatever fragment type you give it. This allows it to become your best friend in changing the fragment at position 0 to the fragment you desire. 2) Notice the listeners that are placed in the 'newInstance(listener)constructor. These are how you will callfragment0Changed(String newFragmentIdentification)`. The following code shows how you create the listener inside of your fragment.
static nextFragmentListener listenerSearch;
public static Fragment_Journals newInstance(nextFragmentListener listener){
listenerSearch = listener;
return new Fragment_Journals();
}
You could then call the change inside of your onPostExecute
private class SearchAsyncTask extends AsyncTask<Void, Void, Void>{
protected Void doInBackground(Void... params){
.
.//some more operation
.
}
protected void onPostExecute(Void param){
listenerSearch.fragment0Changed("searchResultFragment");
}
}
This would trigger the code inside of your viewpager to switch your fragment at position zero fragAt0 to become a new searchResultFragment. There are two more small pieces you would need to add to the viewpager before it became functional.
One would be in the getItem override method of the viewpager.
#Override
public Fragment getItem(int index) {
switch (index) {
case 0:
//this is where it will "remember" which fragment you have just selected. the key is to set a static String fragment at the top of your page that will hold the position that you had just selected.
if(fragAt0 == null){
switch(fragment0){
case "searchFragment":
fragAt0 = FragmentSearch.newInstance(listener);
break;
case "searchResultsFragment":
fragAt0 = FragmentSearchResults.newInstance(listener);
break;
}
}
return fragAt0;
case 1:
// Games fragment activity
return new CreateFragment();
}
Now without this final piece you would still get a blank page. Kind of lame, but it is an essential part of the viewPager. You must override the getItemPosition method of the viewpager. Ordinarily this method will return POSITION_UNCHANGED which tells the viewpager to keep everything the same and so getItem will never get called to place the new fragment on the page. Here's an example of something you could do
public int getItemPosition(Object object)
{
//object is the current fragment displayed at position 0.
if(object instanceof SearchFragment && fragAt0 instanceof SearchResultFragment){
return POSITION_NONE;
//this condition is for when you press back
}else if{(object instanceof SearchResultFragment && fragAt0 instanceof SearchFragment){
return POSITION_NONE;
}
return POSITION_UNCHANGED
}
Like I said, the code gets very involved, but you basically have to create a custom adapter for your situation. The things I mentioned will make it possible to change the fragment. It will likely take a long time to soak everything in so I would be patient, but it will all make sense. It is totally worth taking the time because it can make a really slick looking application.
Here's the nugget for handling the back button. You put this inside your MainActivity
public void onBackPressed() {
if(mViewPager.getCurrentItem() == 0) {
if(pagerAdapter.getItem(0) instanceof FragmentSearchResults){
((FragmentSearchResults) pagerAdapter.getItem(0)).backPressed();
}else if (pagerAdapter.getItem(0) instanceof FragmentSearch) {
finish();
}
}
}
You will need to create a method called backPressed() inside of FragmentSearchResults that calls fragment0changed. This in tandem with the code I showed before will handle pressing the back button. Good luck with your code to change the viewpager. It takes a lot of work, and as far as I have found, there aren't any quick adaptations. Like I said, you are basically creating a custom viewpager adapter, and letting it handle all of the necessary changes using listeners
Here is the code all together for the TabsPagerAdapter.
public class TabsPagerAdapter extends FragmentPagerAdapter{
Fragment fragAt0;
fragmentChangeListener listener = new fragmentChangeListener();
FragmentManager manager;
static String fragment0 = "SearchFragment";
//when you declare the viewpager in your adapter, pass it the fragment manager.
public viewPager(FragmentManager fm) {
super(fm);
manager = fm;
}
private final class fragmentChangeListener implements nextFragmentListener {
#Override
public void fragment0Changed(String fragment) {
//I will explain the purpose of fragment0 in a moment
fragment0 = fragment;
manager.beginTransaction().remove(fragAt0).commit();
switch (fragment){
case "searchFragment":
fragAt0 = SearchFragment.newInstance(listener);
break;
case "searchResultFragment":
fragAt0 = Fragment_Table.newInstance(listener);
break;
}
notifyDataSetChanged();
}
}
#Override
public Fragment getItem(int index) {
switch (index) {
case 0:
//this is where it will "remember" which fragment you have just selected. the key is to set a static String fragment at the top of your page that will hold the position that you had just selected.
if(fragAt0 == null){
switch(fragment0){
case "searchFragment":
fragAt0 = FragmentSearch.newInstance(listener);
break;
case "searchResultsFragment":
fragAt0 = FragmentSearchResults.newInstance(listener);
break;
}
}
return fragAt0;
case 1:
// Games fragment activity
return new CreateFragment();
}
#Override
public int getCount() {
// Show 3 total pages.
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
String[] tab = {"Journals", "Charts", "Website"};
switch (position) {
case 0:
return tab[0].toUpperCase(l);
case 1:
return tab[1].toUpperCase(l);
case 2:
return tab[2].toUpperCase(l);
}
return null;
}
public int getItemPosition(Object object)
{
//object is the current fragment displayed at position 0.
if(object instanceof SearchFragment && fragAt0 instanceof SearchResultFragment){
return POSITION_NONE;
//this condition is for when you press back
}else if{(object instanceof SearchResultFragment && fragAt0 instanceof SearchFragment){
return POSITION_NONE;
}
return POSITION_UNCHANGED
}
public interface nextFragmentListener {
public void fragment0Changed(String fragment);
}

Add different fragment to an activity based on Intent-given identifier

I'm working on an application where in layout layout-small-portrait I want to launch different fragments contained in a single "container activity", named SingleActivity. I will handle this differnetly in layouts layout-land, layout-large etc. but that is unrelated to my problem.
I have an activity MainActivity which is, as the name indicates, the main activity (launcher) of my application. This will initially contain a ListFragment with different items for the user to press.
Based on the item that the user presses the SingleActivity will launch and its content will correspond to a specific Fragment related to this item. My problem starts here. When the user presses an item I have a reference to the corresponding fragment I want to be displayed in SingleFragment. Illustrated below:
String tag = myFragmentReference.getTag();
Intent i = new Intent(this, SingleActivity.class);
i.putExtra(SingleActivity.CONST_TAG, tag);
startActivity(i);
The activity launches successfully. In SingleActivity I have the following onCreate() method:
...
// Retrieve the fragment tag from the intent
String tag = getIntent().getStringExtra(CONST_TAG);
Fragment fragment = getSupportFragmentManager().findFragmentByTag(tag);
if(fragment == null) {
// always end up here, this is my problem.
}
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.fragmentContainer, fragment);
ft.commit();
...
I suspect that the fact that fragment is always null is because the fragment has not been inflated yet. If I am right what I need to do is define a fragment's tag before it is inflated, so that it can be found by findFragmentByTag(). Is that possible?
If anything is unclear please let me know.
I look forward to hearing some good ideas! If there are better or more clever ways to implement this I would love to hear your thoughts! Thanks :)
Since you are jumping to another activity, it will have its own Fragment BackStack and that fragment will not exist.
You will have to inflate the fragment in the new activity something along these lines:
String tag = intent.getStringExtra(CONST_TAG);
if (getSupportFragmentManager().findFragmentByTag(tag) == null) {
Fragment fragment = Fragment.instantiate(this, tag, extras);
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.fragmentContainer, fragment, tag);
ft.commit();
}
The tag string will need to have the package location of the fragment such as "com.android.myprojectname.myfragment"
First use SlidingMenu library: https://github.com/jfeinstein10/SlidingMenu
This will help you, and your app will be more cool, that´s the only way that I can help you make what you need so, here is the code:
Here is your MainActivity:
I´ll try to explain this sample code and you use for your need.
This is the ListFragment of your BehindContent (SlidingMenu):
public class ColorMenuFragment extends ListFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.list, null);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
String[] colors = getResources().getStringArray(R.array.color_names);
ArrayAdapter<String> colorAdapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, android.R.id.text1, colors);
setListAdapter(colorAdapter);
//This array is only to fill SlidingMenu with a Simple String Color.
//I used MergeAdapter from Commonsware to create a very nice SlidingMenu.
}
#Override
public void onListItemClick(ListView lv, View v, int position, long id) {
//This switch case is a listener to select wish item user have been selected, so it Call
//ColorFragment, you can change to Task1Fragment, Task2Fragment, Task3Fragment.
Fragment newContent = null;
switch (position) {
case 0:
newContent = new ColorFragment(R.color.red);
break;
case 1:
newContent = new ColorFragment(R.color.green);
break;
case 2:
newContent = new ColorFragment(R.color.blue);
break;
case 3:
newContent = new ColorFragment(android.R.color.white);
break;
case 4:
newContent = new ColorFragment(android.R.color.black);
break;
}
if (newContent != null)
switchFragment(newContent);
}
// the meat of switching the above fragment
private void switchFragment(Fragment fragment) {
if (getActivity() == null)
return;
if (getActivity() instanceof FragmentChangeActivity) {
FragmentChangeActivity fca = (FragmentChangeActivity) getActivity();
fca.switchContent(fragment);
} else if (getActivity() instanceof ResponsiveUIActivity) {
ResponsiveUIActivity ra = (ResponsiveUIActivity) getActivity();
ra.switchContent(fragment);
}
}
}
Here is your BaseActivity Class:
It dont have swipe, as I could understand, you don't need this.
public class FragmentChangeActivity extends BaseActivity {
private Fragment mContent;
public FragmentChangeActivity() {
super(R.string.changing_fragments);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// set the Above View
if (savedInstanceState != null)
mContent = getSupportFragmentManager().getFragment(savedInstanceState, "mContent");
if (mContent == null)
mContent = new ColorFragment(R.color.red);
// set the Above View
//This will be the first AboveView
setContentView(R.layout.content_frame);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.content_frame, mContent)
.commit();
// set the Behind View
//This is the SlidingMenu
setBehindContentView(R.layout.menu_frame);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.menu_frame, new ColorMenuFragment())
.commit();
// customize the SlidingMenu
//This is opcional
getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
getSupportFragmentManager().putFragment(outState, "mContent", mContent);
}
public void switchContent(Fragment fragment) {
// the meat of switching fragment
mContent = fragment;
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.content_frame, fragment)
.commit();
getSlidingMenu().showContent();
}
}
Ok, So If you want to change the ColorFragment to anything else, do this:
First, choice the item that you want to use:
case 0:
newContent = new ColorFragment(R.color.red);
break;
to:
case 0:
newContent = new ArrayListFragment();
break;
I have made just a arraylist, it is just a simple example, you can do a lot of thing, then you can read about Fragment to learn how to do different things.
public class ArrayListFragment extends ListFragment {
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, Listnames.TITLES));
//Listnames is a class with String[] TITLES;
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
Log.i("FragmentList2", "Item clicked: " + id);
String item = (String) getListAdapter().getItem(position);
Toast.makeText(getActivity(), item, Toast.LENGTH_LONG).show();
}
}
As you see, it can display a different fragment based on which item in the ListFragment (MainActivity) the user presses.
Well, if you misunderstood something, just tell me.

Categories

Resources