There is some synch process on server that updates the database and I want to refresh the parent activity on click of dialogue dismiss to get the synched values in a view.
I have three fragments- FragmentA.java, FragmentB.java and ragmentC.java
In FragmentA.java, I have public interface OnEntrySelectedListener which have method getDialog() and
other fragment class FragmentB.java implements this interface and have definition for getDialog() method.
FragmentA.java
class FragmentA extends Fragment
{
public interface OnEntrySelectedListener
{
getDialog();
}
}
FragmentB.java
class FragmentB extends FragmentActivity implements FragmentA.OnEntrySelectedListener
{
#Override
public void getDialog(Bundle bundle) {
FragmentC cf = new FragmentC();
cf.setArguments(bundle);
cf.show(getSupportFragmentManager(), "dialog");
}
}
FragmentC.java
class FragmentC extends DialogFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
//some code here
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
view = inflater.inflate(R.layout.shared,container, false);
view.findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0)
{
getDialog().dismiss(); //Here after dismiss, I want to refresh FragmentA
}
});
return view;
}
Now I want to refresh the FragmentA on dismiss of FragmentC.
You can add an interface and callback to C to do something like this:
#Override
public void onDismiss(DialogInterface dialog) {
if (getActivity() != null && getActivity() instanceof Dismissed) {
((Dismissed) getActivity()).dialogDismissed();
}
super.onDismiss(dialog);
}
public interface Dismissed {
public void dialogDismissed();
}
Then in the main activity you can implement the interface and when you get the call you forward it to fragment A.
You could also fire a refresh event that FragmentA would listen to.
Related
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//
}
});
Now, it is impossible to use startActivityForResult.
I have to use a launcher, but I don't know what to do because it's a click event that happens in the item view and I'm going to put the image in the item view
I tried various methods, but they were all for activities, so they didn't work.
You need either to cast the context to Activity, or to pass the onClickListener to the adapter as following
Create the clickListener
public interface MyClickListener {
void onItemClick(MyModel model);
}
In your adapter class:
public class MyAdapter extends RecyclerView.Adapter<ViewHolder> {
private MyClickListener onClickListener;
public MyAdapter(MyClickListener onClickListener) {
this.onClickListener = onClickListener;
}
...
}
In your method:
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
onClickListener.onItemClick(myModel);
}
});
In your activity:
MyAdapter adapter = new MyAdapter(new MyClickListener() {
public void onItemClick(MyModel model) {
/// here you can call startActivityForResult
}
});
I have a DialogFragment with a simple cancel button on it.
What I want to do is to dismiss the dialog everytime after pressing the cancel button.
However, I found that if I click the button very quickly once the dialog opened,
the dismiss() method seems not working and the dialog will stuck there like...forever..
My code( Details simplified ):
1.Custom DialogFragment that extends DialogFragment
public class MyDialogFragment extends DialogFragment {
private Callback callback;
// An interface for implementing functions triggered by onClick event.
public interface Callback{
void onCancelClick();
}
// onClick event injection using butterKnife
#OnClick(R.id.button_cancel)
void onClick(View view) {
callback.onCancelClick();
}
// create a new instance
public static MyDialogFragment newInstance() {
MyDialogFragment fragment = new MyDialogFragment();
Bundle args = new Bundle();
fragment.setArguments(args);
return fragment;
}
/**
Lifecycle begins
*/
#Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof Callback) {
callback = (Callback) context;
}
}
#NonNull
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
return dialog;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.my_dialog_fragment, container, false);
ButterKnife.bind(this, view);
return view;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Dialog dialog = getDialog();
dialog.setCancelable(false);
dialog.setCanceledOnTouchOutside(false);
}
}
the host activity.
public class MyActivity extends AppCompatActivity implements
MyDialogFragment.Callback {
private MyDialogFragment myDialogFragment;
private boolean isTaskRunnable = false;
/**
* Call some API
*/
private void callMyApi() {
// Create and show the dialog represents data loading.
myDialogFragment = MyDialogFragment.newInstance();
myDialogFragment.show(getSupportFragmentManager(), "myDialogFragment");
TaskRunnable taskRunnable = new TaskRunnable<XXXXXX>() {
#Override
public responseList doLongOperation(params) throws MyException {
isTaskRunnable = true;
//...... asynchronous execution
return //....
}
#Override
public void callback(responseList response) {
super.callback(response);
if (isTaskRunnable) {
isTaskRunnable = false;
// dismiss the dialog (worked)
if (myDialogFragment != null) {
myDialogFragment.dismiss();
}
}
#Override
public void onFailure(AsyncStatus.ErrorReason reason, String msg) {
super.onFailure(reason, msg);
if (isTaskRunnable) {
isTaskRunnable = false;
// dismiss the dialog (worked)
if (myDialogFragment != null) {
myDialogFragment.dismiss();
}
}
}
};
taskRunnable.setParams(params);
AsyncManager.runBackgroundTask(taskRunnable);
}
/**
* Dismiss() sometimes doesn't work
* when I press the button immediately after openning the dialog.
*/
#Override
public void onCancelClick() {
isTaskRunnable = false;
AsyncManager.cancelAllTasks();
myDialogFragment.dismiss();
}
}
I'd really appreciate it if anyone could help me point out the problem or give me some clues.
I know it's not solution but pretty cheap workaround but when I have time related problem like that I just put code which causes problem inside of Handler().postDelayed(problemedFunc, 500) like that
Not to answer your question but found something weird.
Should myDialogFragment = myDialogFragment.newInstance(); be myDialogFragment = MyDialogFragment.newInstance();?
I'm new to android and I'm trying to get a hang of creating and using Fragments.
I have a fragment that shows a simple list of multiple dates to choose from and implements an onClickListener. The idea is once a user chooses a date, the fragments sends the date back to the MainActivity which then runs a query in database and sends the database response to another fragment.
I'm stuck on the point of sending the date back to MainActivity, elegantly. I can't find much info. I found this:
Activity activity = getActivity();
if(activity instanceof MyActivity){
MyActivity myactivity = (MyActivity) activity;
myactivity.myMethod();
}
I'm very new to this but this seems hacky to me. Is this the right way or is there another way?
Any input is appreciated
I prefer the interface based approach because is very clean. You can declare a nested interface in your Fragment or an external one:
interface OnMyStuffListener {
void myMethod();
}
Make the Activity to implement that interface:
public class MainActivity extends Activity implements OnMyStuffListener {
#Override
public void myMethod() {
// Do whatever you want.
}
}
The Fragment will be attached to the Activity so you can check the instance of the Context and cast it to the Activity:
public class MyFragment extends Fragment implements View.OnClickListener {
private OnMyStuffListener mListener;
#Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnMyStuffListener) {
mListener = (OnMyStuffListener) context;
} else {
throw new IllegalArgumentException("The context " + context.getClass().getName() +
"must implement " + OnMyStuffListener.class.getName());
}
}
#Override
public void onDetach() {
super.onDetach();
// Release it avoiding memory leak.
mListener = null;
}
#Override
public void onClick(View v) {
mListener.myMethod();
}
}
YES this is absolutely right. You can use this, if you are not sure that your Fragment is attached to Activity
You can also achieve this by using Interface, using an EventBus like LocalBroadcastManager, or starting a new Activity with an Intent and some form of flag passed into its extras Bundle or something else.
Here is an example about using Interface:
1. Add function sendDataToActivity() into the interface (EventListener).
//EventListener.java
public interface EventListener {
public void sendDataToActivity(String data);
}
2. Implement this functions in your MainActivity.
// MainActivity.java
public class MainActivity extends AppCompatActivity implements EventListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public void sendDataToActivity(String data) {
Log.i("MainActivity", "sendDataToActivity: " + data);
}
}
3. Create the listener in MyFragment and attach it to the Activity.
4. Finally, call function using listener.sendDataToActivity("Hello World!").
// MyFragment.java
public class MyFragment extends Fragment {
private EventListener listener;
#Override
public void onAttach(Activity activity)
{
super.onAttach(activity);
if(activity instanceof EventListener) {
listener = (EventListener)activity;
} else {
// Throw an error!
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_my, container, false);
// Send data
listener.sendDataToActivity("Hello World!");
return view;
}
#Override
public void onDetach() {
super.onDetach();
listener = null;
}
}
Hope this will help~
I have a MainActivity, and I want to attach a fragment with 3 buttons in it to that activity. On clicking button 1 it should replace this fragment with another fragment. But when I change orientation the current fragment and the old fragment are both getting attached. Can someone help me solve this ?
Following is my MainActivity.java:
public class MainActivity extends AppCompatActivity implements OnButtonsClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportFragmentManager().beginTransaction()
.add(R.id.mainactivity, new FragmentHomepage(), "aboutMe")
.commit();
}
#Override
public void button1Action() {
FragmentAboutMe fragmentAboutMe=new FragmentAboutMe();
getSupportFragmentManager().beginTransaction()
.add(R.id.mainactivity,fragmentAboutMe)
.commit();
}
#Override
public void button2Action() {
Intent intent =new Intent(this,ActivityMasterDetail.class);
startActivity(intent);
}
#Override
public void button3Action() {
Intent intent =new Intent(this,ActivityViewPager.class);
startActivity(intent);
}
}
This is my FragmentHomepage.java:
public class FragmentHomepage extends Fragment {
private static final String ARG_SECTION_NUMBER ="section number";
OnButtonsClickListener onButtonsClickListener;
public static FragmentHomepage newInstance(int sectionNumber){
FragmentHomepage fragmentHomepage=new FragmentHomepage();
Bundle args=new Bundle();
args.putInt(ARG_SECTION_NUMBER,sectionNumber);
fragmentHomepage.setArguments(args);
return fragmentHomepage;
}
public FragmentHomepage(){}
#Override
public void onAttach(Context context) {
super.onAttach(context);
try {
onButtonsClickListener= (OnButtonsClickListener) context;
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
final Button button1= (Button) getActivity().findViewById(R.id.aboutMe);
final Button button2= (Button) getActivity().findViewById(R.id.task2);
final Button button3= (Button) getActivity().findViewById(R.id.task3);
button1.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
onButtonsClickListener.button1Action();
}
});
button2.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
onButtonsClickListener.button2Action();
}
});
button3.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
onButtonsClickListener.button3Action();
}
});
}
#Nullable
#Override
public View onCreateView(final LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
View rootView=null;
rootView =inflater.inflate(R.layout.fragment_homepage,container,false);
return rootView;
}
}
and my second activity is as follows
(in FragmentAboutMe.java):
public class FragmentAboutMe extends Fragment {
int counters=0;
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(savedInstanceState==null)counters=0;
else counters=savedInstanceState.getInt("count");
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("count",13);
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_aboutme,container,false);
}
}
In your onCreate() method put a check whether the fragment is already present. Android automatically restores the fragment manager state upon orientation change and hence upon orientation change, the fragment which you added on button click would automatically be added. Thus if you will add new fragment in onCreate() without check, this would result in adding the 2 fragments. This is causing the issue.
FragmentManager fm = getSupportFragmentManager();
if (fm.findfragmentById(R.id.mainactivity) == null) {
FragmentHomepage fragment = new FragmentHomepage ();
fm.beginTransaction().add (R.id.mainactivity, fragment).commit();
}
You would want to save the state. You would need to extend Fragment to store the objects that need saving.
Override the onConfigurationChanged(..) and it should work out.
When I had to handle screen orientation changes, I recall having used this reference [link].
There are two ways.
Either you need to save the state in the bundle in onSaveInstanceState() method. Also save the states of fragment.
When activity recreate itself then check the value of bundle inside onCreate() or onRestoreInstanceState() method. Now initialize all the objects as before.
Else
set the value of activity inside manifest file as
android:configChanges="layoutDirection|orientation|screenLayout|screenSize"
and override the method.
#Override
public void onConfigurationChanged(Configuration newConfig) {
}
Second technique will not allow the activity to restart on orientation change.
I have a problem that I don't know how to solve. How do you hide a toolbar in a specific fragment, I have already been searching around on the internet and what I found was communicating activity and fragment would solve it. But it doesn't work for me at all, here is my code:
main_activity:
public class MainActivity extends ActionBarActivity implements like_frag.OnHideToolbar{
....
public void onHidingToolbar(int position){
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
if(toolbar == null){
getSupportActionBar().hide();
}else{
getSupportActionBar().hide();
}
}
like_frag.java
public class like_frag extends Fragment {
OnHideToolbar mCallback;
Toolbar toolbar;
public interface OnHideToolbar {
public void onHidingToolbar(int position);
}
public void onAttach(Activity activity){
try{
mCallback = (OnHideToolbar) activity;
}catch(ClassCastException e){
throw new ClassCastException(activity.toString() + "error implementing");
}
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View rootView = inflater.inflate(R.layout.swipefrag, container, false);
toolbar = (Toolbar)getActivity().findViewById(R.id.toolbar);
return rootView;
}
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
}
thanks in advance.
I have a drawer inside the toolbar.
Put this code in fragment in which you want to hide toolbar...
#Override
public void onResume() {
super.onResume();
((AppCompatActivity)getActivity()).getSupportActionBar().hide();
}
#Override
public void onStop() {
super.onStop();
((AppCompatActivity)getActivity()).getSupportActionBar().show();
}
In the fragment's onCreate method call:
((AppCompatActivity) getActivity()).getSupportActionBar().hide();
Replace AppCompateActivity with the activity class you used.
Edited:
You could simply use the onResume method to call hide() and the onStop method to call show() as suggested in some of the comments.
#Override
public void onResume() {
super.onResume();
((AppCompatActivity)getActivity()).getSupportActionBar().hide();
}
#Override
public void onStop() {
super.onStop();
((AppCompatActivity)getActivity()).getSupportActionBar().show();
}
If you are using the new Navigation Component, add this while setting up the toolbar
navController.addOnDestinationChangedListener(new NavController.OnDestinationChangedListener() {
#Override
public void onDestinationChanged(#NonNull NavController controller,
#NonNull NavDestination destination, #Nullable Bundle arguments) {
if(destination.getId() == R.id.full_screen_destination) {
toolbar.setVisibility(View.GONE);
bottomNavigationView.setVisibility(View.GONE);
} else {
toolbar.setVisibility(View.VISIBLE);
bottomNavigationView.setVisibility(View.VISIBLE);
}
}
});
And for Kotlin, you can do the following:
navController.addOnDestinationChangedListener { _, destination, _ ->
if(destination.getId() == R.id.full_screen_destination) {
toolbar.setVisibility(View.GONE)
bottomNavigationView.setVisibility(View.GONE);
} else {
toolbar.setVisibility(View.VISIBLE)
bottomNavigationView.setVisibility(View.VISIBLE);
}
}
Create an interface in the fragment and use it to tell the parent activity to hide the toolbar.
Add these lines to your fragment:
private OnEventListener listener;
public interface OnEventListener {
void hideToolbar() ;
}
public void setOnEventListener(OnEventListener listener) {
this.listener = listener;
}
After creating your fragment in the main activity add:
myFragment.setOnEventListener(new MyFragment.OnEventListener() {
#Override
public void hideToolbar() {
getSupportActionBar().hide();
}
});
Whenever you need to hide the toolbar execute:
listener.hideToolbar();
from inside your fragment.
Just add these methods to the fragment where you want to diable the toolbar ,and also in the fragment's onStop() make it visible again.
#Override
public void onResume() {
super.onResume();
((AppCompatActivity)getActivity()).getSupportActionBar().hide();
}
#Override
public void onStop() {
super.onStop();
((AppCompatActivity)getActivity()).getSupportActionBar().show();
}
in kotlin hide and show supportActionBar as follows:
override fun onResume() {
super.onResume()
(activity as AppCompatActivity).supportActionBar?.hide()
}
override fun onStop() {
super.onStop()
(activity as AppCompatActivity).supportActionBar?.show()
}
and if you want to have your own custom toolbar, in OncreateView set:
//your Custom toolbar in xml
val toolbar = binding.toolbar
(activity as AppCompatActivity).setSupportActionBar(toolbar)
Simply use supportActionBar?.hide() or supportActionBar?.show().
If you are using NavigationController:
navController.addOnDestinationChangedListener { controller, destination, arguments ->
if (destination.id == R.id.loginSuccessFragment) {
supportActionBar?.hide()
} else {
supportActionBar?.show()
}
}
Put this code in fragment in which you want to hide toolbar...
Add this( ((AppCompatActivity)getActivity()).getSupportActionBar().hide();) in onCreateView or in onResume.
and do this in onDestroy()
#Override
public void onDestroy() {
super.onDestroy();
((AppCompatActivity)getActivity()).getSupportActionBar().show();}
use getSupportActionBar().hide(); and getSupportActionBar().show(); in lifeCycle methods
You can try it.
#Override
public void onDestinationChanged(#NonNull NavController controller, #NonNull NavDestination destination, #Nullable Bundle arguments) {
if (destination.getId() == R.id.nav_dashboard){
if (toolbar !=null){
toolbar.setVisibility(View.GONE);
}
}else {
toolbar.setVisibility(View.VISIBLE);
}
}