Android get the app component from getIntent() - java

Okay I got my Application and my MainActivity. When the user presses my app icon my application launches and MainActivity gets created. Now in onCreate() with getIntent() method can i get the Android System component(the class name) which started my activity??

You can get class name by using this code.getClass().getName()
In getIntent() you receive parse variable throw intent.

Related

java.lang.IllegalStateException error :"Can not perform this action after onSaveInstanceState"

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 ?

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 - resuming from last visited page in my app

I have login page (activity) which is my first activity (landing page) of the app.
Only after user gives correct username and password, the app allow you go to other pages to work with.
Now my question is, now the user is at the fourth page and he is working on it, And he minimize the app and use other app like messaging etc..
so when he come back to click my app icon again, my app starts from the first landing page, which is the login page,
how can I make my app to resume from the fourth page?
Android 4.4.4, Kit Kat, Samsung
Regards,
Nay TK
Add this in android manifest of loginActivity,
android:launchMode="singleTask"
more on android:launchMode
An instruction on how the activity should be launched. There are four modes that work in conjunction with activity flags (FLAG_ACTIVITY_* constants) in Intent objects to determine what should happen when the activity is called upon to handle an intent. They are:
"standard"
"singleTop"
"singleTask"
"singleInstance"
The system creates the activity at the root of a new task and routes
the intent to it. However, if an instance of the activity already
exists, the system routes the intent to existing instance through a
call to its onNewIntent() method, rather than creating a new one.
go through official link for more detail
You can make your fourth activity as your launch activity.
In the Resume of your Activity, you check if the user is logged in. If not, you start your Login activity.

Start activity from service and get result from activity [duplicate]

This question already has answers here:
start a remote service and receive a result in Android
(2 answers)
Closed 9 years ago.
How I can get response from Activity, which start from my Service?
I start my Activity in Service's code:
Intent intent = new Intent( context, ActivityMy.class);
intent.putExtra( "iii", iii);
context.startActivity( intent);
startActivityForResult available from Activity but no Service.
How I can get response from Activity?
Update: I need not only send one value from Activity to Service, I need run immediately my code when Activity is finished and return result.
You can create a custom Class that extends of Application and set it in Manifest (just set the android:name attribute in <application>). In this class you can put a static variable called activity and set everytime you change the activity. So in your Service you will can access the activity by using MyApp.activity
The simplest way is to use BroadcastReceiver to send broadcast from Activity to Service.

Application lose current activity after 1 hour

My application is quite simple, I have a few activities (a LoginActivity which is the launcher and the main activity). Then I have some other activities and finally an activity RouteActivity which launches a service.
The LocationService extends Service and startForeground with a notification. The service just starts a LocationListener and register every new GPS location point.
The service appears to work just fine, if I touch the notification icon it bring me back to my application activity from where it was started (not from the Login activity).
Now here is the problem, if I touch the application icon (on the Android launcher) it sometimes lauch my app to the right and current activty RouteActivity, but after around 1 hour, if I touch the application icon it just restart the application from the beginning and start the LoginActivity.
But if I touch my service notification if bring me back to the right and background activity.
Also my service is not killed, never, so it seems to work just fine, objects and variable tied to the application are still there.
So what ? I have 2 instances of my application running ? I'm kinda lost on this one, especially that it seems to be time related.
This "bug" is produced on Android 2.X, I can't reproduce it on Android 4.X. It is kinda hard to debug because I have to let the application run for around 1 hour. And after that time I have no special message in logcat.
I noted something:
The ActivityManager messages are quite strange, if I launch my application through the service notification in the notification center it log:
Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x14000000 cmp=com.wayzup.wayzupapp/com.wayzup.activity.RouteActivity bnds=[0,149][320,213]
While if I launch it trough the application icon it's logged, but the actual activity shown is not the login one but the actual RouteActivity which actually launched the service. (After around 1 hour it is effectively the LoginActivity which is started).
Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.wayzup.wayzupapp/com.wayzup.activity.LoginActivity bnds=[3,338][77,417]
Each time I launch the RouteActivity I also have this log
Activity idle timeout for HistoryRecord{44e78808 com.wayzup.wayzupapp/com.wayzup.activity.RouteActivity}
This is related to my other question: Android foreground service lose context
But I think this one is the real problem and more accurate.
I can post some code if you want to.
It sounds like you're relying on an activity task stack to maintain all your state. I suggest you use instead use something like PreferenceManager.getDefaultSharedPreferences() to maintain the logged in / logged out state. That way your can survive if its activities are terminated and recreated.
The way I tend to do it is have your main activity be the one you want the user to see once they're logged in. In its onCreate() check to see if the user is logged in and if not, startActivityForResult() to send them off to the login activity. Persist the logged in state somewhere so that you can check it in the main activity's onCreate().
I finally resolved my problem.
Now it does not matter if the activity is killed or not, as long as the service is alive it's fine.
In my LoginActivity I check for if my service, if it's running I start the RouteActivity which is bound to it and singleTop. Si I always have the single and same instance of it.
#Override
public void onResume(){
super.onResume();
if (checkMyServiceRunningOrNot()){
restoreAcitivty();
}
}
public void restoreAcitivty(){
Intent intent = new Intent(this, RouteActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}

Categories