Cannot resolve Method getSupportFragmentManager() or ((FragmentActivity)activity)getSupportFragmentManager() - java

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);

Related

Receive URL intent in Splash Screen and load the URL in WebView

I'm having a slightly different purpose, but, I think I'm putting it wrong as no one from multiple forums is able to answer it. The original question is here: Pass URL data from AppLink to WebView
Basically, suppose I'm creating a web browser app with a splash screen and I want to accept the URL intents from other apps, receive them in my splash screen, pass them on to my WebView activity and load it there, how can I do that?
For example, if a user has my app installed, and he/she taps https://www.google.com/ as a link in some app, how can I load the URL in my app after showing my splash screen? I think, the intent receiver will be in the splash screen activity, and the WebView is in another activity. So, basically, I want to receive the URL in my splash screen activity and then, pass it on to my WebView. How to achieve this?
I think what youre trying to do is to recieve data from other apps
That way, you can recieve data in a Splash Screen Activity and then show it in a Web View Activity
You can do this through putExtra method.
You can use intents, which are messages sent between activities. In an intent you can put all sort of data, String, int, etc.
In your case, in Splash Screen(say SplashActivity), before going to next activity(say MainActivity), you need to store a String message this way :
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
intent.putExtra("message", message);
startActivity(intent);
In MainActivity, in onCreate(), you can get the String message by retrieving a Bundle (which contains all the messages sent by the calling activity) and call getString() on it :
Bundle bundle = getIntent().getExtras();
String message = bundle.getString("message");
Then you can use message variable as url.:
Hope this helps !

Android prevent going back to previous activities

I am developing on an Android app in which I want to prevent going back to SignIn activity after user logged in. I have tried Intent.FLAG_ACTIVITY_CLEAR_TASK, but it doesn't work
Intent intent = new Intent(SignInActivity.this, FirstPage.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
And I have also tried adding finish right after start Activity, but that simply gives me a leaked window error.
Intent intent = new Intent(SignInActivity.this, FirstPage.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
The path of my activities is:
MainActivity(description of my app) <-> SignInActivity(sign in) -> First(logged in).
User can go back and forward between MainActivity and SigInActivity. How can I prevent users going back to SignInActivity or MainActivity from FirstPage?
Any idea? Thank you!
Calling finish() will end the activity and not have it on the stack as an activity you can go back to. You need to look into what is causing your leaked window error (such as leaving a notification on screen when you finish the activity) and resolve that rather than not using finish because you have a leaked window error.
With the FLAG_ACTIVITY_NEW_TASK your activity will become the start of a new task of the history stack. Try to use both flags (NEW_TASK and CLEAR_TOP).
You also could override your onBackPressed method.
#Override
public void onBackPressed() {
return;
}
This post has solved a very similar problem, worth to look at it!

Restarting a fragment activity onreceive in broadcast receiver

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.

Passing data from activity to child activity real time

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.

How to remove all activity in flow from stack, when calling some new activity in android

I have a problem in my android app, it having the log out functionality in setting screen.
When we Logout it opens the login screen. But when i press back button then it show the setting screen page, which is not required (as it takes me to inside the app without login). I am using the following code but it is not working. Because at the time of logout LoginActiviy is not exist.
Intent intent= new Intent(HomeSetting.this,LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
Please suggest me some solution, that LoginActivity get call as a new Activity, and all activity in history will get destroyed.
You can set noHistory property of Activity in manifest file as true.So it will be removed from Activity satck,when it goes to background.
To avoid this, you should set flags as follows:
Intent intent= new Intent(getApplicationContext() , LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

Categories