At first glance, your problem seems to be how are you defining your variable activity
. If you are using your variable activity
to cast the context
of Dialog
with getContext()
to Activity
, it will cause a casting error since the Dialog is handled with ContextThemeWrapper
and can cause you as a result:
java.lang.ClassCastException: android.view.ContextThemeWrapper can not
be cast to android.app.Activity
Now, if you could pass your context in a constructor of your ViewDialog
and that context you casted it to your MainActivity
or what is called the Activity linked to your Layout activity_main.xml
:
. . .
private MainActivity activity;
public ViewDialog(Context context){
this.activity = (MainActivity)context; // Solo podras hacer esto si tu ViewDialog lo crea la actividad MainActivity, de lo contrario devolvera null
}
. . .
And even if you receive the null
, you must make sure that the id is the one that belongs to your root that loads this activity, since that way it should work. But still, the problem is the following
The direct interaction between the Activity and a Context that is very far from it is usually a tedious process using onActivityResult
, so many choose to create a class singleton
for communication with an Activity. It works quite well, especially when you try to get a callback from an Activity somewhere in my application that can not capture that event in a "natural" way, such as onBackPressed
.
If you are using many references of your Activity
out of it. The moment you change that reference, you should make changes on all sides where you used those references, so your code would not be very maintainable or would not support "small" changes.
Use an interface to send the value
The recommended thing here is to use an interface that communicates your action from your Dialog
to the Activity. If the activity handles too many interfaces you can handle the interaction interfaces in a clase
to part, and your Activity
that implements the interfaces that your clase
creates or capture the data in onNewIntent
by Bundles
(for this you need to define certain flags before starting your activity again.)
An example of how to create an interface and implement it:
In your ViewDialog
you do this: Define an interface.
public interface OnTextViewDataSet{
void onDataSet(String text);
}
A private variable:
private OnTextViewDataSet listener = null;
And in your constructor:
. . .
public ViewDialog(Context context){
this.activity = (MainActivity)context; // Solo podras hacer esto si tu ViewDialog lo crea la actividad MainActivity, de lo contrario devolvera null
if(activity instanceof OnTextViewDataSet)
this.listener = (OnTextViewDataSet)activity;
}
. . .
In your onClick
you invoke the interface:
@Override
public void onClick(View view) {
String text = "MLL/H";
if(listener != null)
listener.onDataSet(text);
}
And in your MainActivity
or whatever the Activity is where you want to change the text, you implement the interface, example:
public MainActivity extends Activity implements ViewDialog.OnTextViewDataSet
And you overwrite the method of your interface:
@Override
public void onDataSet(String text) {
KMLabel.setText(text);
}
Where KMLabel
is your global reference of TextView
and should not be null. So you can be sure that if you ever remove or change the KMLabel reference, your Activity is responsible for making the necessary changes and you will not have to change the reference in all the classes where you used it with a forced "findViewById".
You can read more in the official documentation of the interaction with the Activitys .