I made an app worked in background using background service when the user clicked specific button (start button) , the user can stop the application task by restart this application and open it again .. then the user can stop the app by clicking on the stop button.
I use this code snippet to let the app close its UI and return to home screen and before that set the start button on disable mode ...
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Unfortunately, when I open my app again I start from the main activity which is not contains the start button and stop button and when I swipe to the second activity that contains the buttons I find the start button enabled i.e. the previous task of the background service is lost ??!!
could any one provide me by the solution.
Every time you open your app the buttons are enabled by default(excepting those which are disabled from the start).
So you need to saved this state to retain it's state.
Here
is how to retain and restore the state.
You have to take a look at Android application lifecycles, when your app goes to the background it will get to the state onPause() and later to onStop(), what you want is saving your application state for later use, take a look at this thread: Saving Activity state in Android I think the top answer is what you want to do.
You have to save the status of your button, and then load the status later.
You could use SharedPreferences to store your wanted activity.
Then check it right after MainActivity start, to decide change to new activity or not
//check when start app
public class MainActivity extends Activity{
public static SharedPreferences sharedPreferences;
#Override
protected void onCreate(Bundle savedInstanceState) {
...
sharedpreferences = getSharedPreferences("WantedActivity", Context.MODE_PRIVATE);
if (sharedPreferences.contains("SavedActivityName"))
{
if(sharedPreferences.getString("SavedActivityName","").equals( "ActivityName"))
{
Intent intent = new Intent(getApplicationContext(), ActivityName.class);
startActivity(intent);
}
}
}
}
//save wanted activity
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("SavedActivityName", "ActivityName");
editor.commit();
Related
I searched a lot but i did not find my answer. I have developed an android application where on the very first lunch user will be shown a welcome screen made of viewpager. The problem is i don't know which place is the best to put the welcome activity code in my application.
The simplest way it could be that in the main activity at the very fist line even before super.onCreate(), inside onCreate method where i try to get the shared preference value and then evaluate whether it is fist lunch. If it is, then i start the welcome activity as shown below
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
boolean welcome = sharedPreferences.getBoolean(getString(R.string.key_welcome), true);
if (welcome) {
// go and start welcoming activity
Intent intent = new Intent(this, WelcomeSlideActivity.class);
startActivity(intent);
}
super.onCreate();
}
}
But i found another approach to deal with it. It is Application class. Since Application class is the first one, which runs even before any other codes in my application. So i thought, i would be nice to do it there as shown below
public class App extends Application {
#Override
public void onCreate() {
super.onCreate();
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
boolean welcome = sharedPreferences.getBoolean(getString(R.string.key_welcome), true);
if (welcome) {
// go and start welcoming activity
Intent intent = new Intent(this, WelcomeSlideActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}
}
So i am in dilemma, which one would be the best option to choose. And i am even not sure if i am doing it in the right way since there is no such documentation in android developer website or anywhere.
Have a look at how to create splash screens the correct way. https://www.bignerdranch.com/blog/splash-screens-the-right-way/
As for using the Application class - this is primarily used for Application-wide configuration for maintaining a global application state. Hence starting an activity from here does not make much sense as it's purpose has changed into becoming an entry point into the application rather than providing state for the app as a whole.
Furthermore, why not make the WelcomeSlideActivity the first 'launcher' activity? Then in there, you can create the logic of whether to launch the next activity without history or whether to show the current view.
Ideally, you should create a splash screen activity, which determines whether to show the WelcomeSlideActivity Or the MainActivity. The advantage of this is that while he app determines which Activity to launch, the user is presented with a splash screen that informs the user that the app has started
In fact, I am new to Android App Development. In my application, I have a couple of activities and I have provided my users with an exit option menu to be able to leave the application. But there is a problem. When they hit the Exit button, they are able to leave the application but when they enter the application for the second time, the page that they left off the last time will be launched.
Here comes my code:
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch (item.getItemId()) {
case 0 :
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
Toast.makeText(this, "Goodbye Dear", Toast.LENGTH_LONG).show();
break;
Android Activity has two methods onPause and onDestroy where you can do the necessary cleanup.
http://developer.android.com/reference/android/app/Activity.html
Instead of using finish(), use System.exit(0);.
You have to override onPause and/or onDestroy methods inside your activity and delete your view within these methods.
The problem in your code is that Intent.FLAG_ACTIVITY_NEW_TASK doesn't remove your current Task. Read more about it here: Task and Back Stack | Android Developers.
Try using Intent.FLAG_ACTIVITY_CLEAR_TOP. From the documentation we can see that this gives the desired behavior.
If set, and the activity being launched is already running in the
current task, then instead of launching a new instance of that
activity, all of the other activities on top of it will be closed and
this Intent will be delivered to the (now on top) old activity as a
new Intent.
I am developing android Application having number of activities. The requirement of the application is When i "FORCESTOP" the application manually(Settings->Applications->ManageApplication->APP_NAME->FORCESTOP), the last focused activity(UI Page) to be displayed. Can i get the Last focused Activity after *FORCESTOP*ing the Application? Please help me with the sample code.
You should keep track of each activity change and save it to file (e.g. in your sharedpreferences) immediately when the change happens. Something like this
protected void onResume() {
SharedPreferences.Editor edit = PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext()).edit();
edit.putString("LastActiveActivity", this.getClass().getName());
edit.commit();
super.onResume();
}
Make sure you save the current activity in the onResume() method of all your activities.
When you restart you can read the last activity back from your preferences. This should be done in the onCreate() method of your main activity (the one that is specified as "LAUNCHER" in your manifest). Then start the activity with the name you've read back.
I have an android app that has various activities.
for example there is a home screen and an update screen.
The home screen is a list activity.
A button on the home screen brings you to the update screen that updates the database from a server. In theory when Returning to teh homescreen after an update, the list should be changed to reflect the update just done.
the code for going to the update screen is as follows:
Button synch = (Button) findViewById(R.id.synchButton);
synch.setOnClickListener(new OnClickListener() {
public void onClick(View viewParam) {
Intent intent = new Intent(HomeScreen.this, SynchScreen.class);
startActivity(intent);
}
});
and the code for returning back to the homescreen after the updates is:
main_menu.setOnClickListener(new OnClickListener() {
public void onClick(View viewParam) {
finish();
}
});
the list is compiled from an async task that runs in onStart, so my understanding is that onStart should run when I return to the homescreen, thus always displaying the most up to date version of the list.
On my Emulator I always get teh updated list, but when I run it on my phone the list is never updated, it just returns to the state of teh screen before I did the update.
any ideas?
thanks
Kevin
Check the Activity lifecycle section of the Android documentation. The code updating the view should probably be moved to onResume, since the Activity might not get killed when launching a new one.
Put the code for starting the Asynctask in onResume. Read the documentation related to activity life cycle.
#Override
public void onResume() {
super.onResume();
Apaptor.refreshListView(Vector);
}
My app has login, once the access is granted, it creates a new html file to the sdcard, the output or the appearance of that html file depends on the account of the logger.
After writing, the app goes to the next activity
Intent nextActivity = new Intent(MyMainAct.this, AppView.class);
startActivity(nextActivity);
the AppView class is the next activity in which the UI is a webview. It views the html created on login.
When I click "login as different user" button, I go back to the Main Activity which is MyMainAct. I use the following code:
Intent nextActivity = new Intent(AppView.this, MyMainAct.class);
startActivity(nextActivity);
When I logged in again (as different user), I expected a different UI since the html created is different. But it did not happen. The same html of the first logger is presented by the webview.
I looked into the html code created by the second logger, it is different from the first. but the webview presents the html of the first logger.
Is it about instance state? That's what I think, that's why I don't want to save instance state (bundle).
EDIT:
How do I ignore savedInstanceState of my App?
my MainActivity:
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
my AppView Activity:
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
Make sure that when user chooses the "login as different user" option, activity showing the HTML is finished (i.e. call the finish method on it). If you then ignore the savedInstanceState parameter in its onCreate event (which you likely do, I presume), the next time this activity is started it will be totally new instance of it.
Try this and see if it works.
Intent nextActivity = new Intent(AppView.this, MyMainAct.class);
startActivity(nextActivity);
finish();