Reputation: 525
I am creating an activity which shows a list of videos(VideoListingActivity) depending on the parameters passed from the parent activity.my I am trying to pass the bundle passed to VideoListingActivity to its child fragment so that it can fetch the related data from database and display it on the activity. I am trying to use Fragment.setArguments() to pass the bundle to fragment but getting the below error .
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.rrdtech.vidyavaan.android/com.rrdtech.vidyavaan.android.VideoListingActivity}: java.lang.IllegalStateException: Fragment already active
I think the fragment is attached to activity post to onCreate() so should this error even be raised ?
Below are the codes for my activity and fragment
VideoListingActivity.java
public class VideoListingActivity extends ActionBarActivity implements FilterFragment.OnFilterItemSelectedListener,
android.support.v7.widget.SearchView.OnQueryTextListener
{
public VideoListFragment videoListFragment;
private android.support.v7.widget.SearchView searchView;
private Context context;
private Bundle bundle;
private String listBy;
private String subjectId;
private String levelType;
private FragmentTransaction ft;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_listing);
// videoListFragment=new VideoListFragment();
videoListFragment= (VideoListFragment) getFragmentManager().findFragmentById(R.id.frg_video_listing_video_listing_fragment);
ft =getFragmentManager().beginTransaction();
context=this;
bundle = new Bundle();
bundle = getIntent().getExtras();
videoListFragment.setArguments(bundle);
}
@Override
protected void onStart() {
super.onStart();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_video_listing, menu);
MenuItem menuItem=menu.findItem(R.id.item_action_search);
//SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView = (android.support.v7.widget.SearchView) menu.findItem(R.id.item_action_search).getActionView();
setupSearchView(menuItem);
//searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
return super.onCreateOptionsMenu(menu);
}
public void setupSearchView(MenuItem searchItem)
{
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
if(searchManager != null)
{
SearchableInfo info = searchManager.getSearchableInfo(getComponentName());
searchView.setSearchableInfo(info);
}
searchView.setOnQueryTextListener(this);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onFilterItemSelected(String filterId) {
videoListFragment.filterSelectedAction("filter id is "+filterId);
}
@Override
public boolean onQueryTextSubmit(String query) {
CommonFunctions commonFunctions = new CommonFunctions();
commonFunctions.showProgressDialog(context,"getting data","bringing");
ft.replace(R.id.frg_video_listing_filter_fragment,videoListFragment);
ft.commit();
commonFunctions.dismissProgressDialog();
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
return false;
}
}
VideoListingFragment.java
public class VideoListFragment extends Fragment {
private ListView videoListView;
private Context activityContext;
private VideosListAdapter listAdapter;
private List<Video> videosList;
private Bundle b;
private String listBy;
private String subjectId;
private String levelType;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
activityContext = activity.getApplicationContext();
b=new Bundle();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.video_list_fragment,container,false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
b= getArguments();
listBy = b.getString(VidyavaanGlobal.listBy);
Log.d("list by ",listBy);
}
@Override
public void onStart() {
super.onStart();
}
public void filterSelectedAction(String x)
{
Log.d("passed is ", x);
}
public void fillData()
{
videosList.add(new Video("chapter1","title1", "pragyan public school", "pawan saraskar", "Chemistry", "Advance", "abcd url", "21", "5/6/2015", "thumbnail url"));
videosList.add(new Video("chapter2","title2","pragyan public school","pawan saraskar","physics","Advance","abcd url","21","5/6/2015","thumbnail url"));
videosList.add(new Video("chapter3","title3","pragyan public school","pawan saraskar","english","Advance","abcd url","21","5/6/2015","thumbnail url"));
videosList.add(new Video("chapter4","title4","pragyan public school","pawan saraskar","Chemistry","Advance","abcd url","21","5/6/2015","thumbnail url"));
videosList.add(new Video("chapter5","title5","pragyan public school","pawan saraskar","Chemistry","Advance","abcd url","21","5/6/2015","thumbnail url"));
videosList.add(new Video("chapter6","title6","pragyan public school","pawan saraskar","Chemistry","Advance","abcd url","21","5/6/2015","thumbnail url"));
videosList.add(new Video("chapter7", "title7", "pragyan public school", "pawan saraskar", "Chemistry", "Advance", "abcd url", "21", "5/6/2015", "thumbnail url"));
}
public void displayVideoList(boolean searchReqeust )
{
}
public void displayVideoList(String pListBy, String pValue)
{
listAdapter = new VideosListAdapter(activityContext,videosList);
videoListView.setAdapter(listAdapter);
}
public void displayVideoList(String subjectId, String videoType, String VideoLevel)
{
}
}
can one please help. Thanks in advance.
Upvotes: 0
Views: 2174
Reputation: 2727
try to call ft =getFragmentManager().beginTransaction();
after adding bundle as arguments.
Upvotes: 0
Reputation: 2727
call
bundle = new Bundle();
bundle = getIntent().getExtras();
videoListFragment.setArguments(bundle);
before
ft =getFragmentManager().beginTransaction();
line.
Upvotes: 0
Reputation:
From the Official Android development Reference:
Public void setArguments (Bundle args) Supply the construction arguments for this fragment. This can only be called before the fragment has been attached to its activity; that is, you should call it immediately after constructing the fragment. The arguments supplied here will be retained across fragment destroy and creation.
Your fragment is already attached to its activity i suggest you to use your own method, you don't need setArguments
!
create your own setUIArguments(Bundle args)
inside the fragment class and update the fragment UI inside this method
You will call this method in this way:
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
Detailfragment detailFragment = (Detailfragment)getFragmentManager().findFragmentById(detailFragmentID);
Bundle bundle = new Bundle();
bundle.putSerializable(BUNDLE_KEY, obj);// passing this object
detailFragment.setUIArguments(bundle); /* your new method */
}
in your fragment class
public void setUIArguments(Bundle args) {
getActivity().runOnUiThread(new Runnable() {
public void run() {
/* do your UI stuffs */
}
}
}
Upvotes: 2