Not able to update new content in webview in fragment in android - android

I have added two fragment in layout. One fragment is list view and one fragment is webview. In webview first showing default data. If the user clicks on list view data, I am not able to update new content in webview in fragment in android. Can anybody tell me what the problem is? How do I do this? See below my code. It's working in the emulator but not on the device.
public void onItemClick(AdapterView<?> parent, View view, int posistion,
long id) {
// TODO Auto-generated method stub
checkTablet=isTablet();
if(checkTablet)
{
webFragment=(WebViewFragment1)getSupportFragmentManager().findFragmentById(R.id.webFrag);
//webView=(WebView)getActivity().findViewById(R.id.loadWeb);
//WebViewFragment1 webViewFragment=(WebViewFragment1).findViewById(R.id.loadWeb);
//webFragment=(WebViewFragment1)getActivity().getSupportFragmentManager().findFragmentById(R.id.webFrag);
DataGenerator dataGenerator=new DataGenerator();
String data=dataGenerator.getData(posistion);
System.out.println("data is"+data);
//webView.loadData(data, "html/data", "utf-8");
webFragment.loadWeb.loadData(data, "html/data", "utf-8");
}
});
Thanks

In my opinion you need to add a fragmentTransaction, just like this:
android.support.v4.app.FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.webFrag, your fragment);
ft.setTransition(android.support.v4.app.FragmentTransaction.TRANSIT_FRAGMENT_CLOSE);
ft.commit();

Related

Open fragment from inside another fragment [duplicate]

This question already has an answer here:
Android: Opening a Fragment from another
(1 answer)
Closed 6 years ago.
I am working inside a Fragment that has a ListView. When the user clicks on a list row, I want to open another fragment that should show another ListView.
This is the method I have for now:
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String topic = String.valueOf(parent.getItemAtPosition(position));
Log.d("Comunidad",topic);
//PASA VALOR SELECCIONADO AL SIGUIENTE FRAGMENT
}
});
What is the best way to open the new Fragment from inside this method?
Thank you.
Add a FrameLayout to your preferred activity's layout to call FragmentA (the fragment to be opened onClick):
<FrameLayout
android:layout_width="match_parent"
android:id="#+id/outer_frame"
android:layout_height="wrap_content">
</FrameLayout>
and then replace outer_frame (FrameLayout) with your FragmentA by doing this:
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String topic = String.valueOf(parent.getItemAtPosition(position));
Log.d("Comunidad",topic);
getSupportFragmentManager().beginTransaction()
.replace(R.id.outer_frame, new FragmentA())
.commit();
}
});
inside onClick of listview
Fragment2 new_frag = new Fragment2();
return new_frag;
// Inside your onClick method
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container, new MyFragment());
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
I might be wrong but i think what you want to do is not possible, because fragments are not intended to be open inside an already opened activity...
What you could do is prepare the activity with the fragment and then change it's content when you want to...
Might be wrong tho...

Android Fragment opening another fragment

I am new to android development, and situation here is simple:
There's a fragment that's being called by MainActivity, where the fragment is being replaced with the frameLayout. And now, what I want here is to open up a fragment that contains the detail information from the following onItemClick.
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Fragment w = new statDetail();
getActivity().getSupportFragmentManager().beginTransaction().add(w,"detail").addToBackStack(null).commit();
}
//From MainActivity
#Override
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
FragmentManager fragmentManager = getSupportFragmentManager();
if (position==0) {
fragmentManager.beginTransaction()
.replace(R.id.container, ExpenseFragment.newInstance(position + 1))
.commit();
}
else {
fragmentManager.beginTransaction()
.replace(R.id.container, StatFragment.newInstance(position + 1))
.commit();
}
}
Above is the code that I'm trying to accomplish to open up another fragment, but it simply stops the whole program. OnItemClick works fine here, and I have already tried just creating a new activity, but because this uses the pre-made navigationdrawer that android studio provides, pressing back button would come to the first page when I wanted to show up the 2nd page, which I have no idea how to modify. I feel like fragment would be easier to use here, but I really don't know.
In the future, I want to implement the back navigation and would appreciate if anyone can help me solve the problem! Thanks!

Show Fragment like a popup with a Transparent background

I would like to make a Fragment pop-up like an AlertDialog but with a transparent background.
My fragment looks like this:
At the moment I am doing the following:
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Player player = (Player)adapterView.getItemAtPosition(i);
FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(PlayerProfileFragment.newInstance(null), null);
fragmentTransaction.commit();
}
The fragment doesn't show at all. I guess in order to make it work, I should use the overload add(layoutId, fragment) but that would just place the Fragment in a specific area and not display the Fragment as a "popup".
Any ideas?
The Fragment probably doesn't show because you are not specifying to which layout you want to add it to. The version of add() that you are using, adds the Fragment to a container whose id is 0 as the docs say.
I fixed this issue by creating an interface with the method:
public void addFragment(int viewId, Fragment fragment);
I then implemented that listener in my Activity by having a FragmentTransaction inside it:
public void addFragment(int viewId, Fragment fragment){
FragmentTransaction fragmentTransaction = getSupportFragmentTransaction().beginTran....;
fragmentTransaction.add(viewId, fragment);
fragmentTransaction.commit();
}

replace nested fragment not working

So, I have a pageView with two tabs (fragments). I want to replace the content of one tab for another fragment.
So, that's what I'm trying:
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Fragment chatFragment = ChatFragment.newInstance();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.replace(R.id.tab_chat_list_container, chatFragment).commit();
}
But nothing happens, when this code is executed, (already check on debug, it's does get called).
Add getChildFragmentManager().executePendingTransactions()

Android: Launching ListFragments as individual fragments from another activity?

I'm working on an app that uses a navigation drawer, with each menu items intended to launch a particular fragment. And each fragment must be in a list view form. Is there a way to launch the listfragments as normal fragment can be launched?
For instance:
new MyListFragment();
Is there a similar way to launch listfragments from the main activity?
Fragments are never "launched," they are added to Activities. Either add the fragment to the current activity using a FragmentTransaction (perhaps by replacing the content area), or start another activity with that fragment in it.
private void goToRadar(FragmentManager fm){
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.flMain, new ChildRadarFragment());
ft.addToBackStack("radar");
ft.commit();
}
You must have a FrameLayout inside the xml-layout of your Activity (here the id = flMain).
You should do the FragmentTransactions when a menu listitem is clicked (=drawer ListView).
mDrawerList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View v, int index,
long arg3) {
// Here the FragmentTransaction
}
});

Categories

Resources