android start activity and keep running previous - java

I would like to call splashScreen activity while current activity is been executed. Once current activity finishes its load, finish splashScreen activity. How can I call one activity and keep executing current activity? Thank you.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent(TabBarActivity.this, splashScreen.class);
startActivity(intent);
setContentView(R.layout.main);
cicle();
}

Have a ProgressBar load in your SplashScreen activity via an AsyncTask. Keep updating the progressbar in that activity and once the loading is fully completed, you can start a new activity in maybe..postExecute() method.
You can refer to this: Progressbar in Splashscreen android

I'd suggest this:
Make the Splashscreen the entry point of your application.
From the SplashscreenActivity execute the long-running task in the background (see AsyncTask and its doInBackground() method)
When that's finished, switch to the next activity and transfer the background operation result (intent.putExtra...)

do the "loading" in splash activity instead, whenever the loading ends, launch the next activity. There is no need for something that complex

Related

Android Java onCreate

So I followed Google's first Android app sample. If I tapped the send button, it opened up the DisplayMessageActivity. But upon tapped the back button (left arrow) from the DisplayMessageActivity, the onCreate(Bundle savedInstanceState) of the MainActivity got called again. It looks like it created a new instance of MainActivity. I could verify this by setting a bool value in onCreate of MainActivity and it was not retained.
How do you go back to the previous instance of MainActivity (the caller)?
You should have a look at Androids Activity Lifecycle.
If you want to access the state of the activity again, I would suggest to use the method
public void onSaveInstanceState(Bundle outState)
to save the current state.
Retrieve the previously saved values in this method:
public void onRestoreInstanceState(Bundle savedInstanceState)`
An example can be found here
You can call finish() in the onClickListener of the back arrow view. It will finish the DisplayMessageActivity and you will return to the caller activity (MainActivity in your case).
Something like:
backArrow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
It looks like it created a new instance of MainActivity.
Yes, I think, that was quite a normal behavior.
Basically, Android OS would keep only one Activity at once so that free as many memory resources as possible.
You should design your application with understanding about such lifecycle concepts.
You can save some of the states of your Activity in certain manners (Parcelable, Bundle or SharedPreferences, etc.).

Start an Activity in full cycle with out showing UI

I have activityB with UI which is working fine as a regular activity with showing UI upon calling from another activityA. Now, I want to call activityB and let the activity complete the entire activity cycle without showing its UI.
So far I had tried:
Set theme in Manifest:
android:theme = "#android:style/Theme.Translucent.NoTitleBar" />
But the problem is I need to setContentView() in ActivityB so that UI element can be recognized in the ActivityB. This ran into error.
Put finish() in activityB onCreate()
The activityB did not complete the full activity cycle. It end at finish().
Any thought about how I can achieve starting an Activity in full cycle without showing its UI.
Thank you
I would suggest you pass some boolean through intent if UI should be shown or not:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
boolean isShowUI = getIntent().getBooleanExtra(EXTRA_IS_SHOW_UI, true);
if (isShowUI) {
setContentView(R.layout.activity_layout);
}
//your onCreate logic
}

Returning to the proper activity when pressing the BACK button?

I'm creating an app with multiple screens the user will have to navigate through. Specifically, I'm currently working on a set of activities that must link together as follows:
Main Activity -> a button click leads to "CreateCharacterActivity" -> a button click leads to "CharacterMainActivity"
The BACK button on "CharacterMainActivity" should lead back to MainActivity without showing the CreateCharacterActivity again.
This behavior should be similar in other areas of the app, except it should restore the state the activity the BACK button leads to was in before it was paused.
So to simplify, I want it like this.
Activity A -> Activity B -> Activity C
BACK button causes Activity C to return to Activity A without going through Activity B.
I tried doing this:
#Override
public void onBackPressed() {
super.onBackPressed();
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
}
But this just invokes a new instance of MainActivity. When I then press BACK at that MainActivity instance, it takes me back to CharacterMainActivity.
How can I achieve this? I'm assuming it involves accessing the Activity stack?
When you move from Activity B to Activity C, call finish() at the same time you call startActivity on Activity C. This will remove Activity B from the task stack.
You can start the child activities for result and then finish the child activities with the result also with the same request code.
To start the child activity:
startActivityForResult (Intent intent, int requestCode)
for finish all the child activity
finishActivity (int requestCode)

How to open not main activity when restart my application in android

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

How do i change a listview on activity resume?

I have a ListView of database items in my main activity, in my main activity i have a method that populates the list view from my database that runs on the oncreate of the main method.
from my main activity the user can go into the menu and click on something called "change view" which is an activity that lets the user change the search criteria for the database, I then have a method that populates the listview from this search criteria.
After the user clicks on the change view button within this activity i want the application to then go back to the main activity and to display this new custom list view, my first thought was to call this method in the on resume, but that doesn't work for the obvious reasons, would appreciate any direction.
you can use startActivityForResult from your main activity or the activity with the list to call the change view activity and then pass the selected option back to the calling activity and use the onActivityResult to read the data returned
and then populate the listview based on it
this link will give you a starting point.or you can use the official reference
You can use custom broadcast receiver for that like below
When you click on the button write this code
Intent i = new Intent("android.intent.action.MAIN")
this.sendBroadcast(i);
finish()//finish your current button activity if this is an activity
Now in onResume of listview activity
IntentFilter intentFilter = new IntentFilter(
"android.intent.action.MAIN");
mReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// Update you listview
}
};
//registering our receiver
this.registerReceiver(mReceiver, intentFilter);
And in onPause you must unregister the receiver like below code
this.unregisterReceiver(this.mReceiver);

Categories