How to change activity, when app is in the background - android

I have an application, that should run some loading (I/O operations) before switching to other activity, that will display loaded information. I would like to load information completly, before switching and this causes some design troubles. I might could have used AsyncTask to run stuff in the background, but from the design perspective I dont want the whole context of the Application be leaked in order to respond to the results. The concept of Service looks more likely to fit my needs.
So let me denote the situation I can't solve from design perspective. Application starts, which causes some Activity to be created and so on. This one creates a service, which does I/O operations in the background. Upon finishing it, Application changes the Activity to the specified one. The question is, how should I handle the situation, when service finishes it's job while the app is in background. As a programmer, I would like to make my app either reopen the first activity and insta change to the following one or simply start with the "following" one. Any ideas?

I would like to make my app either reopen the first activity and insta change to the following one
You could this. When processing in your service finishes save a value in SharedPreference like this
Context ctx = getApplicationContext();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ctx);
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("LoadNewActivity", true); // or false
editor.commit();
Now do this in your launcher activity onCreate()
SharedPreferences prefs = getSharedPreferences(PREFS_NAME, 0);
boolean loadNewActivity = prefs.getBoolean("LoadNewActivity", false);
if(loadNewActivity)
{
Intent intent = new Intent(this, MyNewActivity.class);
startActivity(intent);
}
else
{
// Do normal startup
}

Related

how to continue from the activity that I quit?

I'm Creating A game so I have several Sharedpreference for the score and 2 for check if I was on this activity to not repeat it. So when i quit the app(restart) and reopen it I Want when pressing the start button and it continues from A2 or A3 or ... only if i exited from this activities but my sharedPreference not allowing it Because it's job is to not repeat the activity if the user enter it once. This is my 2 sharedpreference please someone guide me what to do...
SharedPreferences pref = getSharedPreferences("a", Context.MODE_PRIVATE);
if (pref.getBoolean("aa", false)) {
Intent intent = new Intent(this, A2.class);
startActivity(intent);
finish();
} else {
SharedPreferences pref1 = getSharedPreferences("a", Context.MODE_PRIVATE);
SharedPreferences.Editor edt = pref1.edit();
edt.putBoolean("aa", true);
edt.commit();
}
I just want when i exit the app or pause it to continue from where the user Stops
** i don't want to repeat the same activity if the user already answered the question but if he exits without answering I want to continue from where he left**
Thanks
Put a return after your finish statement in the IF loop. Please remember finish does not alter the control flow but it merely asks the system to finish your activity. So if you don't want rest of your code to execute, put in a return statement. I guess rest of your code is fine.

Floating activity draw on other app upon App Launch

I will explain my app first. My app consists of a service running in the background. The service is waiting for a certain app to be opened (spotify in my case).
Whenever Spotify opens it needs to show this popup with buttons and text. It has to basicly look like this: imgur.com/QHWZpu6
I've already tried DialogFragment but that doesn't work since there is no FragmentManager in the service class.
Do you guys have any suggestions? Thanks in advance!
Detect if App is Running/Launched
Take a look at ActivityManager
The method you are concerned with is ActivityManager.RunningAppProcessInfo(). It returns the package names of current running apps as a list. All you have to do is iterate through list and check if it matches app package, which in your case is spotify.
ActivityManager activityManager = (ActivityManager) this.getSystemService( ACTIVITY_SERVICE );
List<RunningAppProcessInfo> appInfos = activityManager.getRunningAppProcesses();
for(int i = 0; i < appInfos.size(); i++)
{
if(appInfos.get(i).processName.equals("package name"))
{
//USE INTENT TO START YOUR ACTIVITY AS EXPLAINED BELOW
}
}
Start Activity from Service
To start activity from your service, use Intent as following:
Intent popUpIntent = new Intent(this, MyActivity.class);
popUpIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(popUpIntent);
Popup Activity / Show as Dialog
In order to display your Activity as a dialog, you just need to set your activity theme to "Theme.Dialog" in your manifest file
<activity
...
android:theme="#android:style/Theme.Dialog"
...
/>
Or you can dynamically set the theme in the activity by calling setTheme() inside Activity class.
setTheme(android.R.style.Theme_Dialog);
A service class is not supposed to interact with the UI thread. A simple interaction to get past this is to use the service thread to update something stored in SharedPreferences and set a shared preferences change listener in the UI thread for whichever activity you want to handle the UI display.
void onSharedPreferenceChanged (SharedPreferences sharedPreferences,
String key)
Based on that image, it seems you actually want to display a UI element when you're activity is not currently the active application, you might want to consider a notification for this instead

How do remember that user signed up and take straight to another Activity?

The scenario is if the user downloads the app for first time, I ask them basic questions (say Activity A) and request them to sign up (Activity B).
This is a one time process only after installing the app. After that whenever they open the app, I am planning to take them straight away in to the app (Activity C).
How should I do this? I am a newbie to Android programming. But I am not able to think about this scenario. I don't want to use database.
You need a persistent storage mechanism to save the state of the user (logged in or not). There are various ways you can do this. The easiest is SharedPreference which will store the user state locally. You can also store this information in your remote server and validate user each time she opens the app although this might be going a bit overboard in most cases.
Try using SharedPreferences. In ActivityA in on create check if SharedPreferences contain a certain value which decides if the user is signed up. If it not set or it does not have the required value, redirect the user to ActivityB or else ActivityC
Code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences pref = this.getSharedPreferences("my_prefs", Context.MODE_PRIVATE);
if(pref.contains("MY_KEY") && pref.getBoolean("MY_KEY", false)){ //first stratup or user has not signed in yet
Intent intent = new Intent(this, ActivityC.class);
startActivity(intent);
} else { //already signed up
Intent intent = new Intent(this, ActivityB.class);
startActivity(intent);
}
setContentView(R.layout.activity_main);
}
Dont forget to save/insert value inside SharedPreferences after the user sign up.
Code:
SharedPreferences pref = this.getSharedPreferences("my_prefs", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
if(sign_up_success){
editor.putBoolean("MY_KEY", true);
editor.commit();
}
The basic flow should be as follows Splash Screen -> OnBoarding Screen(Signup) -> MainActivity On the Splash Screen you need to check for a prefs value lets call it isSignedUp which is false if the user has not signed up. Its pretty straightforward from here. Every time you launch your app you need to check if the preference if isSignedUp is true or false and based on the value show the screen accordingly. So if the value is true that means the user has signed up and show him the main screen else show him the sing up screen.
Use Android SharedPreferences, it can be used to store data which can be used to keep the answers in cache.

Showing UI for android app for the first time and hide it from menu

I am developing an android device anti-theft application in which the user's phone would automatically send a sms to the recovery mobile numbers with new numbers' details on boot. I used SmsManager with BroadCastReceiver and everything was good till that. But I want the UI part to be visible when I install the app to get the recovery numbers from the users only for the first time and then hide the app and only send sms'on boot later(after first time,work only on background). Is there a way to achieve this?
Use PackageManager to disable the Activity that has the main launch intent filter after setup is complete.
So many possibilities which you can try out are available
Use shared preferences in application. This checks whether our application installed is populated with the values that you want to cross check during boot up time
Use SQLITE DB to store values which can be encrypted. Check for the business logic and process accordingly.
Update data in your server and every time app is up , cross check with server data and run your logic.
File system (creating file and writing/reading data) This is the last and not advisable option
If you want references , how to make all these pls refer the following links
Android Shared Preference ExampleAndroid Sqlite exampleAndroid file System example
I guess you need to launch the Activity exactly once after it has been installed. In that case, when that specific activity is launched you can create a SharedPreference and save a value in there! And next time you launch, check if that value exists, if it doesn't launch the activity, else do the other option. Here is how I did it.
In the Activity to be launched once:
SharedPreferences pref = getSharedPreferences("ActivitySession", Context.MODE_PRIVATE);
SharedPreferences.Editor ed = pref.edit();
ed.putString("HASH", hash_received);
ed.putString("MOB",mob_no);
ed.apply();
And then in Activity before that, I implemented:
SharedPreferences pref = getSharedPreferences("ActivitySession", Context.MODE_PRIVATE);
String abc = pref.getString("HASH","");
if(abc.equals(Test))
{
startActivity(new Intent(Splash.this, MainActivity.class));
finish();
}
else
{
startActivity(new Intent(Splash.this, FeedActivity.class));
finish();
}
Hope it helps! Happy coding!

Is there any way to capture the Clear Data action programmatically in application?

I developed one android app which is having sets of activities and one background service running. I am keeping some app data to the device's internal memory so when users clicks on Clear Data option from Settings->Applications-> Clear Data button, all data saved in internal memory gets cleared.
I have to captured Clear Data click event or action(if any available) when user clicks on Clear Data button in my App's service OR in broadcast receiver class, can anyone suggest that is there any action thrown by device upon clicking on Clear data of app so I could catch that action in my app broadcast receiver class and perform desired task???
Please provide me the resolution with some example.
Regards,
Piks
The best way would be storing a value in SharedPreferences like "IS_DATA_CLEARED" when the app is run first or when you save all required details and always make sure this value is never fiddled with at any time during the app's life cycle.
SharedPreferences appSharedPrefs = PreferenceManager
.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = appSharedPrefs.edit();
editor.putBoolean("IS_DATA_CLEARED",false);
editor.apply();
Then every time do the below check.
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(this);
boolean IS_DATA_CLEARED = appSharedPrefs.getBoolean("IS_DATA_CLEARED",true);
if(IS_DATA_CLEARED){
//DATA IS CLEARED
}
else{
//DATA IS NOT CLEARED
}
What you can do is use the following attribute in the application tag in the Manifest File.
android:manageSpaceActivity=".path.to.MyActivity"
This will replace the button "Clear Data" from the settings to "Manage Space".
Now you can redirect the user to a custom activity that is controlled by you.
The launch of the activity will act as callback for you. Manipulate your data in your activity and use the following code
private void clearPreferences() {
try {
// clearing app data
Runtime runtime = Runtime.getRuntime();
runtime.exec("pm clear YOUR_APP_PACKAGE_GOES HERE");
} catch (Exception e) {
e.printStackTrace();
}
}
to clear the app data.
I have to captured Clear Data click event or action(if any available) when user clicks on Clear Data button in my App's service OR in broadcast receiver class, can anyone suggest that is there any action thrown by device upon clicking on Clear data of app so I could catch that action in my app broadcast receiver class and perform desired task???
There is none. AFAIK, pressing Clear Data also stops your process, so your code will not be running. You would treat the user pressing Clear Data as being identical to the case where the user installed your app for the very first time.
Perhaps set up a broadcast receiver in your Manifest and listen for the
"android.intent.action.PACKAGE_DATA_CLEARED"
Intent that is broadcast when an application's data is cleared from settings. You should be able to find out more information about this on the android developers website.
http://developer.android.com/reference/android/content/Intent.html#ACTION_PACKAGE_DATA_CLEARED

Categories

Resources