Invoking methods from MainActivity within the onClick methods of a DialogFragment - java

I'm trying to use a DialogFragment to show a Dialog within my MainActivity. Depending on the user's reaction to the dialog I want to invoke methods defined in my MainActivity.java file (e.g. onActivityResult, but ideally also customized methods).
Following a reply by ashishduh on this question, I defined the DialogFragment as follows (in a seperate java file):
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
public class YesNoDialog extends DialogFragment {
public static final String ARG_TITLE = "YesNoDialog.Title";
public static final String ARG_MESSAGE = "YesNoDialog.Message";
public YesNoDialog() {}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{ Bundle args = getArguments();
String title = args.getString(ARG_TITLE);
String message = args.getString(ARG_MESSAGE);
return new AlertDialog.Builder(getActivity())
.setTitle(title)
.setMessage(message)
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_OK, null);
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_CANCELED, null);
}
})
.create();
}
}
Correspondingly, I try to start it from the MainActivity like this:
public void openYesNoDialog (View view) {
DialogFragment dialog = new YesNoDialog();
Bundle args = new Bundle();
args.putString(YesNoDialog.ARG_TITLE, "title");
args.putString(YesNoDialog.ARG_MESSAGE, "message");
dialog.setArguments(args);
dialog.setTargetFragment(this, YES_NO_CALL);
dialog.show(getSupportFragmentManager(), "tag");
}
where openYesNoDialog is triggered by a button in the activity_main.xml layout file.
I am facing the problem that setTargetFragment(this, YES_NO_CALL) is not working, since "this" corresponds to my MainActivity, but setTargetFragment is (naturally)
expecting a Fragment and no Activity. The problem is that I do not know what to reference in the first argument instead because apart from the DialogFragment I
am trying to build I have made absolutely no use of Fragments in my code.
So I am wondering which of the following strategies you would encourage to fix my issue (not even sure if all of them might possibly work):
1.) Use a method similar to setTargetFragment which allows setting a target Activity. (sort of a "setTargetActivity" method; this solution sounds easiest to me if such a thing exists, but I haven't found anything similar yet).
2.) Write everything in terms of Fragments and have something like a "MainFragment" instead of a MainActivity. I could then easily reference this "MainFragment" as a reasonable target fragment with "this".
3.) Use a completely different approach (e.g. not putting the methods in the activity but in an interface both activity and fragment implement, but actually I also want to make use of e.g. TextViews of the activity inside of the DialogFragment, so I think this might be a problem)
I am very thankful for any help.
One final comment: Note that I am using the v4 support libraries in my imports to support backward compatibility as suggested in the Android tutorials on Dialogs.
This is for example why I needed to use getSupportFragmentManager() instead of getFragmentManager() to make work what is already working right now. So that's the reason for my slight modifications of the code I have been referring to with the hyperlink.

getTargetFragment and setTargetFragment both we should use for communication between Fragment to Fragment,
For Activity to Fragment communication, you can use 2 ways
You can use interface for communication
You can use Local broadcast
Interface communication
Create one interface in dialog fragment,
public class YesNoDialog extends DialogFragment {
public interface OnDialogActionListener {
public void onClickDialog();
}
private OnDialogActionListener mListener;
#Override
public void onAttach(Context context) {
mListener = (OnDialogActionListener) context;
}
// Your code
#Override
public void onClick(DialogInterface dialog, int which)
{
mListener.onClickDialog();
}
}
And in Your activity you can implement and override the function, you will get callback in your Activty.

You can simply use interface for the same. Just define interface in a separate class and declare method as onClickEvent/onSuccess according to you and override it in your activity and perform your task in your activity in the method. And call this method from your dialog on yes/no click buttons.

Related

Implementing a class that will override the onClick()

I want to create a method that, when implemented in other classes, you just need to pass some parameters and then call the - onclick() function to set the element.
At the moment, I´ve just done this. But this gives me a RunTimeException
Unable to start activity ComponentInfo{com.example.test/com.example.testActivity.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.ActivityThread$ApplicationThread android.app.ActivityThread.getApplicationThread()' on a null object reference
my code:
Class: clickeable
imports ...
public class ClickeableOptions implements View.OnClickListener{
private CardView cardView;
private Context cont;
private Class actTarget;
public OpcionesMainClickeables() {}
public ClickeableOptions(CardView cardView, Context cont, Class actTarget) {
this.cardView = cardView;
this.cont = cont;
this.actTarget= actTarget;
}
//Getters and Setters
#Override
public void onClick(View v) {
getCardView().setOnClickListener(this);
Intent intent = new Intent(this.getCont(), this.getActTarget());
startActivity(intent);
}
}
And i want to implement this class like this...
public class MainActivity extends AppCompatActivity{
private CardView cvRegistration;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cvRegistration = (CardView) findViewById(R.id.cvRegistration);
ClickeableOptions optionRegistration = new ClickeableOptions(
cvRegistration, this, Registration.class
);
optionRegistration.onClick(optionRegistration.getCardView());
}
I've implemented the method in the same file, but I want to do it this way to keep things more tidy.
I think the problem is in the use of this, but i really don't get it
No need to call onClick explicitly from your Activity. Just try like below:
public ClickeableOptions(CardView cardView, Context cont, Class actTarget) {
this.cardView = cardView;
this.cont = cont;
this.actTarget= actTarget;
this.cardView.setOnClickListener(this);
}
And inside onClick
#Override
public void onClick(View v) {
Intent intent = new Intent(getCont(), getActTarget());
getCont().startActivity(intent);
}
And remove this line from Activity.
//optionRegistration.onClick(optionRegistration.getCardView());
Now when you clicked your CardView, Then Activity transitions start.
Try to replace this line:
startActivity(intent);
with
getCont().startActivity(intent);
Also, why are you inheriting ClickeableOptions from AppCompatActivity?
Although you have instantiated the ClickeableOptions (derived from Activity) object, none of its Activity life cycle methods have been called and none of the super class instantiation related work (normally should be done in onCreate) has been accomplished. Hence the ActivityThread is simply null and you've got an exception when invoking
startActivity(intent);
If you want to start another Activity on your CardView click then you need to
follow Leo Leontev's advice:
call
getCont().startActivity(intent)
instead of
startActivity(intent)
move the line
getCardView().setOnClickListener(this)
to the ClickeableOptions custom constructor's most bottom line
delete this line from your MainActivity$onCreate() method:
optionRegistration.onClick(optionRegistration.getCardView());
After all of above is done you'll be able to start new activity on click event.

ViewHolder from Adapter from Fragment starts Activity, how can the Activity talk back to the Fragment?

PlaylistFragment starts an adapter:
playlistsAdapter = new PlaylistRecyclerAdapter(playlistsListArray, addToPlaylist, mSong, getActivity(), this);
PlaylistRecyclerAdapter binds data to the PlaylistViewHolder, something like this:
((PlaylistViewHolder) viewHolder).bind(this, dataSet.get(position), addToPlaylist, mSong);
User clicks on an item in PlaylistViewHolder:
context.startActivity(PublicPlaylistActivity.createStartIntent(context, playlist));
Now here is the question, how can PublicPlaylistActivity talk back to the initial PlaylistFragment?
I suggest you'd better use Interface from fragment to adapter. So when user clicks anything in adapter, call function realization in fragment. If you need your activity to proceed some operation - ((YourActivity) getActivity()).someMethod() should be called from fragment.
Second trick is using broadcastreceiver to send events. A bit more complicated. You have to launch broadcast in view you need to recive message and send these messages from adapter. This approach is more complexible to debug and support if system is wide spread, so you'd better use interfaces.
There are several ways of doing that. The simplest way should be starting the PublicPlaylistActivity with startActivityForResult. In that way, then the activity finishes, you can set send some data to the caller fragment (which is PlaylistFragment in your case). Here is a nice tutorial about the implementation.
Another way of doing that is by using lifecycle methods. You might have a public static variable which can keep track of some status that you might observe in your onResume function of your PlaylistFragment when you are returning back from your PublicPlaylistActivity. You might consider a sample implementation as follows.
Define a public static variable in your PlaylistFragment. Then in your onResume function check the value of that variable and take actions accordingly.
public static boolean someIndicator = false; // Initialize with a default value
#Override
protected void onResume() {
super.onResume();
if(someIndicator == true) doSomething();
else doSomethingElse();
}
Now you can set the indicator variable from anywhere in your application actually which will have the effect on your PlaylistFragment. For example, from your PublicPlaylistActivity, you might consider doing something like this.
public void someFunctionInYourPublicPlaylistActivity() {
// ...
// Some code and then the following
PlaylistFragment.someIndicator = true;
}
Another way of achieving the same thing is by using a BroadcastReceiver. Here is a tutorial on how you can implement one.
It really depends on how you are structuring your whole activity-fragments communication. Hope that helps!
I would do a common "context" class (ComContext) with an interface. When you create your fragment, you also create this class. And from the activity you can check if it exists or not.
I assume that you already have a helper(AppHelper) class with static variables.
public class AppHelper {
public static ComContext comContext = null;
}
public class MainFragment {
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ConContext comContext = new ComContext();
comContext.listener = this;
AppHelper.comContext = comContext;
}
#Override
public void onDataChanged() {
}
#Override
public void onDestroyView() {
super.onDestroyView();
AppHelper.comContext = null;
}
}
public class MainActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (AppHelper.comContext != null) {
AppHelper.comContext.listener.onDataChanged();
}
}
}
public class ComContext {
public interface HelperListener {
void onDataChanged();
}
public HelperListener listener = null;
}

How to define a specific layout onclick function globally for multiple activities

I need to define a layout for multiple activities in android and from the UI part, it is successful. But to code those elements to perform on each click listeners, I need to define it in all the java pages I use.
Can we globally define this in a java page and include it in the required pages?
menuButton = findViewById(R.id.menuButton);
menuButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(), MenuActivity.class);
startActivity(i);
finish();
overridePendingTransition(0, 0);
}
});
Yes and No.
You can create a BaseActivity which has the common logic that has to be executed for each button click.
But you need to implement the listener for the button on specific activity, since life cycle of each activity is independent of other activity.
To make the code readable better (avoiding implementing listener/setOnclickListener), you can use ButterKinfe, and create a method for OnClick() annotation, and call the method in BaseActivity.
What you essentially want to do is call the findViewById(), which can only be called if you have a reference to a Context variable. You should use your Activity Context, hence you pass this to the static function, which can then access all methods accessible via Context .
public class ExampleActivity2 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MenuUtils.setListener(this);
}
}
Define the static class like this:
public static class MenuUtils{
public static void setListener(Context context){
menuButton = context.findViewById(R.id.menuButton);
menuButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// do stuff
}
});
}
}
What you should be careful about is that any Activity you pass to this function should have a menuButton in it's layout, otherwise you run the risk of getting a NullPointerException, which is when findViewById() cannot find menuButton.

Fragment null must be a public static class to be properly recreated from instance state

I am not able to figure out why my app crashes when getSupportFragmentManager() is called.I have used similar code in other apps to create alert dialogs without any issues.please help!
DialogFragment df = new DialogFragment(){
#NonNull
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
View view = getActivity().getLayoutInflater().inflate(R.layout.addincome,null);
builder.setView(view);
//capture
final EditText amountEditText=(EditText)view.findViewById(R.id.edit_amount);
final EditText descriptionEditText=(EditText)view.findViewById(R.id.edit_description);
builder.setNegativeButton(android.R.string.cancel,null);
builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
newIncome.setAmount(Double.parseDouble(amountEditText.getText().toString()));
newIncome.setDescription(descriptionEditText.getText().toString());
user.incomes.add(newIncome);
HashMap<String,User> modified = new HashMap<>();
modified.put(uid,user);
rootref.setValue(modified);
}
});
return builder.create();
}
};
df.show(getSupportFragmentManager(),"addIncome");
Your DialogFragment is an anonymous class, and in Java anonymous classes can only be instantiated by parent classes: the new DialogFragment() is in fact this.new DialogFragment(). Apparently, when FragmentManager tries to recreate your DialogFragment upon a configuration change, it can't, since it doesn't have the access to the instance of the parent class. The solution would be to either declare a static subclass of DialogFragment, or to declare a top-level subclass of DialogFragment, and use it instead of the anonymous class.
I know you have done this step but just asking did you tried to rebuild your APK?because if this code works in other applications then their must be some .classes issue.
check which type of Fragment you have in your imports whether your'e using android.support.v4.app.Fragment or android.os.Fragment.

DialogFragment with multiple activities

I used the documentation here to create a dialogfragment. The code is :
public static MyAlertDialogFragment newInstance(int title) {
MyAlertDialogFragment frag = new MyAlertDialogFragment();
Bundle args = new Bundle();
args.putInt("title", title);
frag.setArguments(args);
return frag;
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
int title = getArguments().getInt("title");
return new AlertDialog.Builder(getActivity())
.setIcon(R.drawable.alert_dialog_icon)
.setTitle(title)
.setPositiveButton(R.string.alert_dialog_ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
((FragmentAlertDialog)getActivity()).doPositiveClick();
}
}
)
.setNegativeButton(R.string.alert_dialog_cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
((FragmentAlertDialog)getActivity()).doNegativeClick();
}
}
)
.create();
}}
the dialogfragment here is associated with only the activity FragmentAlertDialog. Is there any way to associate it with multiple activities? I have my calling activity name in onCreateDialog by passing it through setArguements(). Any way to use it ? I checked this question and was hoping for a confirmation/better way.
Instead of having a FragmentAlerDialog activity, you could define somewhere an interface (by somewhere I mean either a public static interface in DialogFragment class, or a separate public interface Java file), and any Activity that wishes to display the dialog could implement this interface.
One common practice that I use is to have a root Activity for all my project activities. Make that root activity implement that interface and then you can display that DialogFragment from anywhere.
I'll just post what edits I made in my code, all credits to #gunar,
create new DialogImplement.java as:
package com.example.test;
public interface DialogImplement
{
public void doPositiveClick();
}
Add #Override in activity code before the implementation of doPositiveClick(), for eg:
#Override
public void doPositiveClick()
{
//do what you want to do
}
make sure your activity implements DialogImplement, and modify code in the question as:
((DialogImplement)getActivity()).doPositiveClick(); //Or negative click code
Hope this helps..Cheers :]

Categories

Resources