Using getApplicationContext() in another simple class, NOT in MainActivity.java - java

I've created a simple class for my Android application called coreTuts. I'm done with tying it in to my MainActivity.java, activity_main.xml, and so on, like this:
MainActivity.java
coreTuts tuts = new coreTuts();
public void displayToast(View view)
{
tuts.sampleToast();
}
This is what my coreTuts.java looks like:
coreTuts.java
public class coreTuts{
//Do a toast notification
public void sampleToast()
{
Toast toast = Toast.makeText(getActivity().getApplicationContext(),
"This is a message displayed in a Toast",
Toast.LENGTH_SHORT);
toast.show();
}
}
I couldn't decide whether I should usegetActivity().getApplicationContext()or just getApplicationContext() on my Toast because either code doesn't work.
In fact, these are my questions:
I understand contexts in Android are kinda like habitats to animals. Am I right if I look at getActivity() and getApplicationContext() that way?
How do I make getApplicationContext() work in another class so that I can run my toast, or is that even allowed?
Thank you!

your coreTuts should be like below
public class coreTuts{
//Do a toast notification
public void sampleToast(Context context)
{
Toast toast = Toast.makeText(context,
"This is a message displayed in a Toast",
Toast.LENGTH_SHORT);
toast.show();
}
}
and you can invoke it like below,
coreTuts tuts = new coreTuts();
public void displayToast(View view)
{
tuts.sampleToast(view.getContext());
}
Note: view cannot be null

As your class CoreTuts is not inheriting from Activity nor any other Context subclass (Activity is a child of Context) you can't access to your context the way you are trying to. You need to explicitly pass it to your sampleToast method, like this:
public class coreTuts{
//Do a toast notification
public void sampleToast(Context context) {
Toast toast = Toast.makeText(context,
"This is a message displayed in a Toast",
Toast.LENGTH_SHORT);
toast.show();
}
}
And in your activity:
coreTuts tuts = new coreTuts();
public void displayToast(View view) {
// Pass your activity as the context
tuts.sampleToast(this);
}

Pass a context to your coretuts class when you're creating it's object. Your coretuts class would look like this.
public class coreTuts{
private Context mContext;
public coreTuts(Context context) {
mContext = context;
}
//Do a toast notification
public void sampleToast()
{
Toast toast = Toast.makeText(mContext,
"This is a message displayed in a Toast",
Toast.LENGTH_SHORT);
toast.show();
}
}
Now, when you're creating an object of this class inside your MainActivity, simply pass the context
// Pass your context. You can also use getApplicationContext() instead of MainActivity.this
coreTuts tuts = new coreTuts(MainActivity.this);
// You don't really need a view argument for this method.
// It could just be public void displayToast() {...}
public void displayToast(View view)
{
tuts.sampleToast();
}

Related

Problems displaying toast message in update and delete method

I would like to know how to put a Toast in each of these functions in the following lines:
Toast.makeText(OwnerAdapter.this,"Owner sucessfully deleted!",Toast.LENGTH_SHORT).show();
Toast.makeText(OwnerAdapter.this,"Owner updated successfully!",Toast.LENGTH_SHORT).show();
These functions are inside the OwnerAdapter class that serve to create a one-dimensional list of elements. But it's not working because the compiler throws an error:
Cannot resolved method 'makeText(com.example.iury.bookapp.OwnerAdapter,java.lang.String, int)
Code Here
public class Owner Adapter extends RecyclerView.Adapter<OnwerAdapter.ViewHolder> {
// Button update
holder.button_update.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String name_owner = holder.editName_owner.getText().toString();
String email_owner = holder.editEmail_owner.getText().toString();
String telephone_owner = holder.editTelephone_owner.getText().toString();
conexao.UpdateOwner(new Owner(f_owner.getId_owner(),name_onwer,email_owner,telephone_onwer));
notifyDataSetChanged();
Toast.makeText(OwnerAdapter.this,"Owner updated successfully!",Toast.LENGTH_SHORT).show();
((Activity) context).finish();
context.startActivity(((Activity) context).getIntent());
}
});
// Button delete
holder.button_delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
connection.DeleteOnwer(f_owner.getId_owner());
owner.remove(position);
Toast.makeText(OwnerAdapter.this,"Owner sucessfully deleted!",Toast.LENGTH_SHORT).show();
notifyDataSetChanged();
}
});
}
You need to pass context to Toast to make it work.
If the adapter is inside an activity, you need to show a message like
Toast.makeText(getApplicationContext(),"Owner updated successfully!",Toast.LENGTH_SHORT);
If the adapter is inside a fragment, you need to show a message like
Toast.makeText(getActivity(),"Owner updated successfully!", Toast.LENGTH_SHORT).show();
If the adapter is written as a separate java file, you can do it like
Toast.makeText(v.getContext(),"Owner updated successfully!",Toast.LENGTH_SHORT).show();
or you could pass context to the adapter and pass that context to Toast
Toast.makeText(context,"Owner updated successfully!",Toast.LENGTH_SHORT).show();

Show alert dialog from background process in any activity

How to get result of background process in any Activity?
I'm working with OTA update. App handle that in background with handler. When update is done I have to show alert dialog to the user. I can show it in SettingsActivity where I start with OTA but user can be in any other Activity.
private void checkIsUpdated() {
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
mDevice.getDevice().read(GattUUID.LS_DEVICE_INFORMATION, GattUUID.LC_FIRMWARE, new
BleDevice.ReadWriteListener() {
#Override
public void onEvent(ReadWriteEvent e) {
if (e.wasSuccess()) {
if (firmwareVersion.equals(e.data_string())) {
showAlertDialog("OTA update failed", "Device is not updated");
} else {
showAlertDialog("OTA update is successful.\nDevice is updated to new " +
"firmware!", "Device is updated");
}
Log.i("OTA", "Read firmware is new success");
} else {
Log.e("OTA", "Read firmware is new success");
}
}
});
}
}, 30000);
}
AlertDialog code
private void showAlertDialog(String message, String title) {
AlertDialog.Builder builder = new AlertDialog.Builder(mContext, R.style.SwipeDialogLight);
builder.setTitle(title)
.setMessage(message)
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
if (!alert.isShowing()) {
alert.show();
}
}
As #W4R10CK stated, I thought that too. The BroadcastReceiver might not a very good idea. But later, I gave a thought about it and if you call checkIsUpdated function inside a Service and send the Broadcast from it, it might be a feasible solution.
So basically you need a BaseActivity class and in which you need to have a BroadcastReceiver. You need to register it onResume and again unregister it onPause.
And you need to have an abstract method to be overriden in each of your Activity too.
So your BaseActivity may look like this..
public abstract class BaseActivity extends AppCompatActivity {
private final Context context;
public BaseActivity() {
this.context = setContext();
}
protected abstract Context setContext();
#Override
public void onResume() {
super.onResume();
registerBroadcastReceiver();
}
#Override
public void onPause() {
super.onPause();
unRegisterBroadcastReceiver();
}
// Declare your BroadcastReceiver here
private class MyBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
showAlertDialog(context);
}
}
}
As #rogerwar suggested in the comments, the correct approach is a broadcast receiver.
Vogella has a nice tutorial
Since you want to have it in all activities, you can make a base class for all your activities and in this base class you can register the receiver in the onStart and unregister it in the onStop.

Callback function did't trigger when calling from DialogFragment to Fragment

Right now i'm having :-
1) 1 activity which is the main activity that extends from AppCompactActivity.
2) 1 fragment class that extends from fragment, this is the fragment that being called from main activity (1) - ProfileTeacherActivity.java
3) 1 fragment class that extends from DialogFragment, this dialog getting called from fragment (2) - ModalBox.java
So, basically, this is just a simple flow of execution. At start, the applications showing the main activity (1) having drawer that have a few links as example a profile link, when click this link, the application call the fragment (2) showing details of profile with one edit button. After clicking edit button, the applications will invoke DialogFragment (3) that contains some of EditText for editing user's profile.
What i want to achieve is, after editing user's profile and successful saved into database, i tried to send user's data back to fragment (2) just to show latest updated info, unfortunately it didn't work.
Here is what i'm tried :
1) Creating Interface inside DialogFragment (3) - ModalBox.java
public class ModalBox extends DialogFragment{
....
public interface EditProfileModalBoxInterface {
void onFinishEditProfile( HashMap<String, String> dataPassing );
}
...
...
}
2) Inside DialogFragment also i have .setPositiveButton function for OK button. - ModalBox.java
public class ModalBox extends DialogFragment{
...
...
public Dialog onCreateDialog(Bundle savedInstanceState ) {
...
builder
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, int id) {
// At here i'm using retrofit2 http library
// to do updating stuff
// and inside success's callback of retrofit2(asynchronous)
// here i call the below function to send data
// dataToSend is a HashMap value
sendBackResultToParent( dataTosend );
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
.....
}
// Function called inside success's callback of retrofit2
public void sendBackResultToParent( HashMap<String, String> data ) {
// instantiated interface
EditProfileModalBoxInterface ls=(EditProfileModalBoxInterface)getTargetFragment();
// declaring interface's method
ls.onFinishEditProfile( data );
}
}
3) Finally, i'm implements those interface inside fragment (2) - ProfileTeacherActivity.java
public class ProfileTeacherActivity extends Fragment
implements ModalBox.EditProfileModalBoxInterface{
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState ) {
.....
.....
}
// At here the interface's method did't triggered
#Override
public void onFinishEditProfile( HashMap dataPassedFromDialog ) {
Toast.makeText(getActivity(), "Testing...." , Toast.LENGTH_LONG).show();
}
}
What i'm confuses right now is, the problem happens only when i called this function sendBackResultToParent( dataTosend ); inside retrofit2 success's callback, it does triggered when calling outside of it. I'm assumed the async called caused this. If i could use Promise or something like that or is there any workaround on this?
The following existing solutions didn't work in my case :
Callback to a Fragment from a DialogFragment
How to send data from DialogFragment to a Fragment?
Send Data from DialogFragment to Fragment
Ask me for more inputs if above use case didn't clear enough or misunderstanding. Thanks for the helps. Regards.
This is a sample DialogFragment code used to send message to selected contact. I too required to capture the click event on the DialogFragment and redirect.
Ideally to achieve this , this is what needed to be done
Override the positive/negative button clicks of AlertDialog.Builder and do no action
After this , using getButton method mention AlertDialog.BUTTON_NEGATIVE or AlertDialog.BUTTON_POSITIVE and assign an action
public class SMSDialogFrag extends DialogFragment {
private static String one="one";
private EditText messageContent;
private AlertDialog dialog;
private String mobNumber;
public static SMSDialogFrag showDialog(String mobNumber){
SMSDialogFrag customDialogFrag=new SMSDialogFrag();
Bundle bundle=new Bundle();
bundle.putString(one, mobNumber);
customDialogFrag.setArguments(bundle);
return customDialogFrag;
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
View view = getActivity().getLayoutInflater().inflate(R.layout.sms_dialog, null);
alertDialogBuilder.setView(view);
setupUI(view);
alertDialogBuilder.setTitle("");
alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//Do nothing here because we override this button later
}
});
alertDialogBuilder.setPositiveButton("Send", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//Do nothing here because we override this button later
}
});
dialog = alertDialogBuilder.create();
dialog.show();
dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
//else dialog stays open. Make sure you have an obvious way to close the dialog especially if you set cancellable to false.
}
});
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
sendMessage();//IN YOUR USE CASE YOU CAN REDIRECT TO YOUR CALLER FRAGMENT
}
});
return dialog;
}
void setupUI(View view){
TextView textViewMob=(TextView)view.findViewById(R.id.mobNumber);
messageContent=(EditText)view.findViewById(R.id.messageContent);
mobNumber=getArguments().getString(one);
textViewMob.setText("Send message to : "+mobNumber);
}
void sendMessage(){
if( ! TextUtils.isEmpty(messageContent.getText())){
try {
SmsManager smsManager = SmsManager.getDefault();
Log.v(Constants.UI_LOG,"Number >>>>>>> "+mobNumber);
smsManager.sendTextMessage(mobNumber, null, messageContent.getText().toString(), null, null);
} catch (Exception e) {
e.printStackTrace();
}
Toast.makeText(getActivity(),"Message Sent!",Toast.LENGTH_SHORT).show();
dialog.dismiss();
}else{
Toast.makeText(getActivity(),"Please enter message to send!",Toast.LENGTH_SHORT).show();
}
}
}
Consider using eventBus, for example
Otto
The usage is very simple. All you need to do is create an evenbus:
public static Bus bus = new Bus(ThreadEnforcer.MAIN); //use Dagger2 to avoid static
Then create a receiver method (in fragment 2 in your case):
#Subscribe
public void getMessage(String s) {
Toast.makeText(this, s, Toast.LENGTH_LONG).show();
}
Send you message by calling(from DialigFramgent):
bus.post("Hello");
And don't forget to register your eventBus inside onCreate method(of your Fragment):
bus.register(this);
And that is!
From architectural standpoint, 2 fragments should not directly communicate with one another. Container Activity should be responsible for passing data between it's child fragments. So here's how i would do it:
Implement your interface in the container Activity and just attach your interface implementation in the Activity to the Dialog class and call that interface method when required. Something like this :
public static class ModalBox extends DialogFragment {
EditProfileModalBoxInterface mListener;
// Container Activity must implement this interface
public interface EditProfileModalBoxInterface {
void onFinishEditProfile( HashMap<String, String> dataPassing );
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (EditProfileModalBoxInterface) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement EditProfileModalBoxInterface");
}
}
}
Then call mListener.onFinishEditProfile(....) where ever it's required in the DialogFragment class.
This way you will receive the result back in your Activity class from where you can call your desired fragment's relevant method to pass the results to that fragment.
This whole flow has been described here
Finally the culprit founds. All the answers mentioned above were right. And my script also actually works, the problem is related with my API json's response that did't coming with right structure. In my case, i'm using retrofit2 with GSON converter for parsing into POJO. Found the info on log saying about :
Expected BEGIN_OBJECT but was BEGIN_ARRAY
Means that, GSON expecting the json object, which is my API was returned JSON array. Just change the API's structure then all goods to go. Thanks guys for your hard time. Really appreciated, as i'm right now knowing how to deal with eventBus, replacing default OK and Cancel button and the correct way for communicating between fragments.

Android - LocalBroadcast

I searched a lot but didn't find the solution to the following issue.
Context :
I use an AsyncHttpResponseHandler object to handle all my webservice
responses
If I get a particular error code from the WS, I want to show an alert dialog (whatever the activity currently displayed)
I came to think that using the LocalBroadcastManager would be a good solution as the HTTP handler is not aware of the currently displayed activity
Problem :
After implementation of all what seems to be needed to make it work, my intent sent from the async handler is not received.
Additional note :
the ApplicationContext is stored in my StaticItems
class which contains all the static data I need in my app.It is setup via a custom class which inherits from Application
If I broadcast the intent from an activity, the OnReceive event is triggered
I thank you in advance for any help you could provide.
Cheers !
Here are some pieces of my code :
The code in the http handler
public class AsyncResponseHandler extends AsyncHttpResponseHandler {
#Override
public void onSuccess(int statusCode, Header[] headers, byte[] response) {
if(response != null && response.length > 0) {
CrvData data = JsonHelper.getCrvData(new String(response));
String code = data.getErrorCode();
if (!TextUtils.isEmpty(code) && code.equals(StaticItems.C_WS_OBSOLETE_VERSION_ERROR)) {
Intent intent = new Intent();
intent.setAction(StaticItems.BROADCAST_INTENT_MESSAGE);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setData(Uri.parse(data.getValue()));
LocalBroadcastManager.getInstance(StaticItems.applicationContext).sendBroadcast(intent);
}
}
}
The code of the root Activity I use for all my activities :
public class MainActivity extends Activity {
/**
* The local broadcast receiver
*/
protected MyBroadcastReceiver mMessageReceiver = new MyBroadcastReceiver();
#Override
protected void onResume() {
super.onResume();
// register
IntentFilter filter = new IntentFilter();
filter.addCategory(Intent.CATEGORY_DEFAULT);
filter.addAction(StaticItems.BROADCAST_INTENT_MESSAGE);
LocalBroadcastManager.getInstance(StaticItems.applicationContext).registerReceiver(mMessageReceiver, filter);
}
#Override
protected void onPause() {
super.onPause();
// Unregister
LocalBroadcastManager.getInstance(StaticItems.applicationContext).unregisterReceiver(mMessageReceiver);
}
}
The receiver
public class MyBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(final Context context, final Intent intent) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setInverseBackgroundForced(true)
.setNegativeButton(context.getResources().getString(R.string.dlg_no), new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.setIcon(R.drawable.ic_dialog_alert_holo_light)
.setMessage(R.string.dlg_app_is_obsolete)
.setPositiveButton(context.getResources().getString(R.string.dlg_yes), new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
context.startActivity(intent);
}
});
builder.show();
}
}
I had a similar problem, for some reason you can not setData(..) on Intent returned via LocalBroadcastManager, when I placed my URI in the extra data, it started to work.... possibly a bug?
My guess is that the context you are using is not an activity context and thus can't be used for creating dialogs.
Namely, the context that is passed to onReceive in this case should be your application context, StaticItems.applicationContext, which is not good for showing dialogs.
If this is the case, you should somehow rearrange your code to make sure to pass an activity context rather than the application one.
Remove addCategory() from your IntentFilter
was solved here
OK so... I figured out that the intent was not received because of this line :
intent.setData(Uri.parse(data.getValue()));
And as #mvai pointed, the dialog cannot be displayed from the application context. I managed to get the current activity by having a reference to it in my application class. (I checked out to be sure for not having any memory leak)

Android - How to show Toast on ConnectionTimeout

I have the following problem: My app uses a function which executes all HTTP calls. As a quick fix I'd like to show the user a toast message whenever there is an ConnectionTimeout.
The problem is that this executeHttp() is called from several AsyncTasks and I can't figure out how to get the required context.
I read something about runOnUiThread but this also didn't seem to work for me...
Any ideas?
Thanks!
Update:
I'd like to have a solution which I can use in my executeHttp() function because else I have do add this code in 100 different places... Is this possible?
First, implement a Method to show a Toast to your activity:
class MyActivity extends Activity {
// some stuff
public void showToast(String text, int duration) {
Toast toast = Toast.makeText(this.getBaseContext(), text, duration);
toast.show();
}
}
The let your AsyncTask hold a reference to the activty which can then be called in the onProgressUpdate Method:
class MyAsyncTask extends AsyncTask {
MyActivity activity;
public MyAsyncTask(MyActivity a)
this.activity = a;
}
#Override
protected Object doInBackground(String... params) {
try {
// do your stuff here
} catch (ConnectionTimeoutException e) {
publishProgress("timeout");
}
#Override
protected void onProgressUpdate(String... values) {
if(values[0].equals("timeout")
activity.showToast("Ups, we have a timeout!", Toast.LENGTH_LONG); }
}
}
}
EDIT ------------
If you want it in your executeHttp() method, you have to pass the Context to it in order to show a Toast:
public ReturnValue executeHttp(Context context) {
try {
// ...
} catch(ConnectionTimeoutException e) {
Toast t = Toast.makeToast(context, message, duration);
t.show();
}
}
summary: no available context -> no toast

Categories