Reputation: 456
How to pass data from a searchview that is in mainActivity, to a fragment? I'm sweating a BottonNavigationActivity.
Already tried:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.menu_search, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
searchView = (SearchView) searchItem.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
Bundle bundle = new Bundle();
bundle.putString("teste", "123");
fragment2.setArguments(bundle);
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
return true;
}
in Fragment:
Bundle bundle = this.getArguments();
if (bundle != null) {
String foo = getArguments().getString("teste");
Toast.makeText(getActivity().getApplicationContext(), foo, Toast.LENGTH_SHORT).show();
}
and too:
Bundle bundle = this.getArguments();
if (bundle != null) {
String foo = bundle.getString("teste");
Toast.makeText(getActivity().getApplicationContext(), foo,
Toast.LENGTH_SHORT).show();
}
And the app to work. can anybody help me?
adding fragment:
Fragment currentFragment;
private home fragment1 = new home();
private clientes fragment2 = new clientes();
private contas_pagar fragment3 = new contas_pagar();
private BottomViewPagerAdapter bottomViewPagerAdapter;
private ArrayList<AHBottomNavigationItem> bottomNavigationItems = new ArrayList<>();
// UI
private AHBottomNavigationViewPager viewPagerBottom;
private AHBottomNavigation bottomNavigation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initUI();
}
private void initUI() {
bottomNavigation = (AHBottomNavigation) findViewById(R.id.bottom_navigation);
viewPagerBottom = (AHBottomNavigationViewPager) findViewById(R.id.view_pager_bottom);
//Aqui onde é adicionado os fragments no bottom
viewPagerBottom.setOffscreenPageLimit(2);
bottomViewPagerAdapter = new BottomViewPagerAdapter(getSupportFragmentManager());
bottomViewPagerAdapter.add(fragment1);
bottomViewPagerAdapter.add(fragment2);
bottomViewPagerAdapter.add(fragment3);
viewPagerBottom.setAdapter(bottomViewPagerAdapter);
currentFragment = bottomViewPagerAdapter.getCurrentFragment();
AHBottomNavigationItem item1 = new AHBottomNavigationItem("Home", R.drawable.ic_home_black_24dp);
AHBottomNavigationItem item2 = new AHBottomNavigationItem("Clientes", R.drawable.clientes);
AHBottomNavigationItem item3 = new AHBottomNavigationItem("Contas", R.drawable.pagar);;
bottomNavigationItems.add(item1);
bottomNavigationItems.add(item2);
bottomNavigationItems.add(item3);
bottomNavigation.addItems(bottomNavigationItems);
bottomNavigation.setAccentColor(Color.parseColor("#F63D2B"));
bottomNavigation.setInactiveColor(Color.parseColor("#747474"));
bottomNavigation.setCurrentItem(0);
//bottomNavigation.setNotification("Vencida", 2);
bottomNavigation.findViewById(R.id.textView18);
bottomNavigation.setOnTabSelectedListener(new AHBottomNavigation.OnTabSelectedListener() {
@Override
public boolean onTabSelected(int position, boolean wasSelected) {
if (currentFragment == null) {
currentFragment = bottomViewPagerAdapter.getCurrentFragment();
}
if (currentFragment != null) {
if (currentFragment instanceof home) {
fragment1.willBeHidden();
} else if (currentFragment instanceof clientes) {
fragment2.willBeHidden();
} else if (currentFragment instanceof contas_pagar) {
fragment3.willBeHidden();
}
}
//Aqui é onde é setado qual o fragment atual
//Em seguida é pego o fragment atual e feito o fade dependendo de qual instancia for
viewPagerBottom.setCurrentItem(position, false);
currentFragment = bottomViewPagerAdapter.getCurrentFragment();
if (currentFragment instanceof home) {
fragment1.willBeDisplayed();
} else if (currentFragment instanceof clientes) {
fragment2.willBeDisplayed();
} else if (currentFragment instanceof contas_pagar) {
fragment3.willBeDisplayed();
}
if (position == 0) {
}
return true;
}
});
bottomNavigation.setOnNavigationPositionListener(new AHBottomNavigation.OnNavigationPositionListener() {
@Override
public void onPositionChange(int y) {
Log.d("DemoActivity", "BottomNavigation Position: " + y);
}
});
}
Adapter:
public class BottomViewPagerAdapter extends FragmentPagerAdapter {
private ArrayList<Fragment> fragments = new ArrayList<>();
private Fragment currentFragment;
public BottomViewPagerAdapter(FragmentManager fm) {
super(fm);
}
public void add(Fragment frag) {
this.fragments.add(frag);
}
@Override
public Fragment getItem(int position) {
return fragments.get(position);
}
@Override
public int getCount() {
return fragments.size();
}
@Override
public void setPrimaryItem(ViewGroup container, int position, Object object)
{
if (getCurrentFragment() != object) {
currentFragment = ((Fragment) object);
}
super.setPrimaryItem(container, position, object);
}
/**
* Get the current fragment
*/
public Fragment getCurrentFragment() {
return currentFragment;
}
}
Logcat:
02-14 13:50:07.435 12250-12250/insidetechnology.studio.ostdor.forbusiness E/AndroidRuntime: FATAL EXCEPTION: main Process: insidetechnology.studio.ostdor.forbusiness, PID: 12250 java.lang.IllegalStateException: Fragment already active at android.support.v4.app.Fragment.setArguments(Fragment.java:562) at insidetechnology.studio.ostdor.forbusiness.MainActivity$2.onQueryTextSubmit(MainActivity.java:196) at android.support.v7.widget.SearchView.onSubmitQuery(SearchView.java:1242) at android.support.v7.widget.SearchView$9.onEditorAction(SearchView.java:1219) at android.widget.TextView.onEditorAction(TextView.java:4983) at com.android.internal.widget.EditableInputConnection.performEditorAction(EditableInputConnection.java:145) at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:364) at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:91) at android.os.Handler.dispatchMessage(Handler.java:110) at android.os.Looper.loop(Looper.java:208) at android.app.ActivityThread.main(ActivityThread.java:6267) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)
Upvotes: 0
Views: 1065
Reputation: 2334
You can use getCurrentFragment()
using your adapter, typecast it to your fragment2 and call the public method where you want to pass the string.
Upvotes: 0
Reputation: 561
The setArguments()
function is used to pass data for the creation of Fragments. To communicate with existing Fragments, you would just call a method of your custom Fragment class itself:
public boolean onQueryTextSubmit(String query) {
if (fragment2 instanceof clientes) {
((clientes)fragment2).setTestString("123");
}
}
You need to add a method setTestString
to your fragment2
like this:
public void setTestString(String text) {
Toast.makeText(getActivity(), foo, Toast.LENGTH_SHORT).show();
}
There's actually a good article in Google's Android Training that also describes how to pass data back to the Activity.
Upvotes: 1
Reputation: 7
You can communicate between activity and fragment by creating an interface callback. Invoke that callback in onQueryTextSubmit and recieve that value in fragment.
Upvotes: 0