Only calling the OnDestroy() of the mainActivity - java

I am building an app that allows you to send files to other apps through a server. The thing is that when one of them closes the app by swiping it off, i want to send a disconnect message to the server.
The problem is that imagine that on the stack i have activities A (MainActivity) , B , C and D. If I am in the activity D and I swipe the app off (turn off the app), it will call the OnDestroy() method from the MainActivity. From the MainActivity, i can't send that disconnect message, so my question is why isn't the onDestroy() from the other activities called ?
PS: I could send the disconnect message from the MainActivity, but what if the person didn't reach the server part of the app? What if the person didn't try the part of the app that lets them send and receive files ? I would be sending a disconnect message for something that doesn't even exist.
Or would there be any way where I could check, from within the MainActivity, if the activity D exists ?

Related

Is there a way I can keep a socket thread running over multiple activities?

I am very new to threading and sockets in Android. Here's what I am trying to do.
Step 1. Activity 1 has a button called "Connect".
Step 2. On clicking the "Connect" button, the app will try to connect to server.java running on the computer. If the connection fails, it will stay in the same activity. Else it will go to Activity 2.
Step 3. Activity 2 has an editbox and a button "Send". On clicking the "Send" button, the data is sent to the server.
Can I access that same thread in the next activity? If the thread is alive, how can I perform tasks on that particular thread? Any sample code will be very helpful.
You should use a foreground service in activity 1 when the connect button starts the socket, actually your service should connects to the socket and service will handle the connection and also sending and receiving data.
after that in activity2 you should use something like eventBus to send data from activity to service and send your data from service to socket
this is solution is the best way to do this because all the data and models handling are done in service and you don't have to process responses in each and every activity using the socket.
hope this helps

How do I change the onPause method of an APK file?

I have an APK file that it requires on Internet connection. But when I click the Home button it loses its connection and can't use the Internet in onPause mode. How can I change the onPause method to do what I want in my application without losing the connection?
For example, I want it to click somewhere to unlock something in a specific time in my app. But at that time I'm sleeping and I can't do that. I just can put my app in onPause mode when I'm sleeping and make a schedule to do my favor.
You better should use Service class for your task. Then kill it calling
stopSevice(getApplicationContext(), YourService.class);
It won't stop unless Android system kill it or You kill it.
For example:
Suppose, you want to download a picture from internet with a button click. Now to download a picture you code a service class.
DownLoadPicture.class
public class DownLoadPicture extends Service
#Override
private void onCreate(){
//Download your picture now here
}
And when your picture is downloaded then just kill your service calling
stopSelf();
Or, you can kill the service from your MainActivity. To kill the service from your MainActivity call
stopService(getApplicationContext(), DownLoadPicture.class);
So, now as you are saying that when your app goes on onPause then you lost internet connection. For solving this:-
Put all your codr in the DownLoadPicture class's onCreate method that you put in your MainActivity to do your internet task. And I hope you won't lost your internet connection then.
And then on your MainActivity's onCreate method put:-
startService(getApplicationContext, DownLoadPicture.class);
And now once your service is started then it won't stop unless you own or Android system kill it

How to read NFC Tag and call Webservice in Android

I'm now developing an android application with the NFC concept . In that application I need to pick my application if I swipe the NFC card and if I select my application my application must start calling webservice.
when it comes to my problem, If suppose my app crashed ,when I swipe the card next time ,the option to choose the application doesn't open .Instead,my application directly launched and couldn't call the webservice data.
On the whole.I'm getting last page when it crashed .But I need to open as fresh
I came to know that I need to make changes in OnResume() and OnNewIntent().
I used ,
In OnResume()
super.onResume();
mNfcAdapter.enableForegroundDispatch(this, nfcPendingIntent, mNdefExchangeFilters, null);
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {
NdefMessage[] msgs = getNdefMessages(getIntent());
byte[] payload = msgs[0].getRecords()[0].getPayload();
//Toast.makeText(this, new String(payload), Toast.LENGTH_LONG).show();
Student=new String(payload);
if(Student.equals(rakesh)
{
new Webservice.execute(""); // Calling SOAP Webservice
}
But,I can't find any changes with my problem .and one more thing that the problem will be resolve after I just open and close an another NFC project
Please help.
Yeah ! I got the solution .I forget a simple thing that I left calling onStop() method and my problem was,when my application stops(when Crashed).It runs in background as the previous activity.
I just tried the following,
public void onStop()
{
super.onStop();
this.finish();
}
This may be helpful to others.
Thanks..

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

How does the default browser on Android send "SEND" intents?

I'd like to enable a "silent send" from my application-- that is, I'd like to catch the SEND intent in a Service, not an Activity. How does the default Android web browser send that intent? Is it possible to handle it in a Service, or in an Activity that never sets a View?
Is it possible to handle it in a
Service, or in an Activity that never
sets a View?
ACTION_SEND Intents are sent as activity intents (e.g., startActivity()), and so you cannot have a Service receive them. You can have them handled by an Activity that never calls setContentView(), though.

Categories