I have an ongoing activity but once I receive a broadcast from the android.net.conn.CONNECTIVITY_CHANGE I wish to display to the users that there is no internet connection from an already active fragment activity
Container app = (Container) context.getApplicationContext();
app.recreate();
But the above code does not work in the onreceive method in my broadcast intent reciever and cause a can not cast android app to fragment activity Container. How do I fix this?
I wish to display to the users that there is no internet connection
from an already active fragment activity
Try having a "BaseFragmentActivity" that has a broadcast receiver member that does this. And each FragmentActivity in your app should extend it.
Related
I am writing an app where i am trying to display a dialog fragment on Activity A. Activity-A launch Activity-B using an intent. I am launching the Dialogfragment using a broadcast intent whose receiver is registered on Activity-A . But when i switch from activity A to activity B , i get the an IllegalStateException error when Activity A broadcast receiver receives the intent . The eror displays : Can not perform this action after onSaveInstanceState . Could you help me please ?
I'm trying to launch an activity directly from a broadcast receiver without the user having to manually open up my app. What I'm hoping to achieve is to automatically open up my app after a device restart. I have a receiver listening for a BOOT_COMPLETED event, which from that I want to open up an activity, or start my app.
Is this possible?
This is the code I'm currently using to launch the activity inside the receiver:
Intent i = new Intent();
i.setClassName(packageName, activityName);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
I want to launch a fragment from my BroadcastReciver class because I want to launch a fragment on a mobile home screen just like a dialog. using that user will stop or snooze the alarm. how to achieve that?
I tried all solutions available at StackOverflow but I can't get an answer. every time I am getting an error.
Cannot resolve Method getSupportFragmentManager()
or
Cannot resolve FragmentActivity in
((FragmentActivity)activity)getSupportFragmentManager()
any suggestion or question? comment below
You need an Activity to host that Fragment. A Fragment cannot be possible without a hosting Activity. At first, create an Activity (eg. AlarmActivity) with transparent background (You can achieve with themes) which has a FragmentContainerView to host your fragment. When you receive broadcast in your BroadcastReceiver, start this activity:
Intent intent = new Intent(context, AlarmActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // This is required
context.startActivity(intent);
I always get bundle value null when I pass data from activity to broadcast receiver.
My receiver will start on boot up.
This is the code in my activity class
Intent intent= new Intent();
intent.setAction("android.intent.action.BOOT_COMPLETED");
intent.putExtra("test", "test");
sendBroadcast(intent);
This is the code in my receiver class:
String testValue = intent.getStringExtra("test");
Your code in activity will never be called upon booting up. System invokes onReceive() with its own intent. You can check this by putting some logs in activity code - this log will not be printed in logcat.
I have a app with a main activity, a graph activity an a Bluetooth ConnectListenerImpl.
The main activity search for a bluetooth device and the Bluetooth ConnectListenerImpl connect with the device and send data Message to the main activity with a Handler.
And that the main activity display the data.
Now i want to display the data in the graph activity which is a child of the main activity.
The start of the child activity.
bGrafiekShow = true;
Intent intent = null;
intent = new Intent(MainActivity.this, GraphActivity.class);
startActivity(intent);
Now is my question how can i do that and what is the best way?
Little extra information. The main activity put the data from the Bluetooth in a other class this class make some calculation. After the calculation is finish the main activity put the result on the screen.
The Bluetooth device sends the data every one second.
Now I want the calculated data plot in a time graph in a child activity. But how can i send the data to the child activity.
You can pass data thought Activities
as map of data presented like Bundle
with putExtra() methods of Intent
with Serializable or Parceable interface.
Or you can use ResultReceiver class. You can combine it with IntentService instead of Handler. In Service you will do your work and any update will be sent into ResultReceiver and receiver will update UI.
Put the device ID in your intent or in a bundle.
intent.putExtra("DEVICE_ID", deviceId);
When starting your second activity, retrieve device Id and connect your "child" second activity to the bluetooth via ConnectListenerImpl.