Start an Activity in full cycle with out showing UI - java

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
}

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.).

In android Studio, I want when i click on button , next activity/fragment should come from right side

In android Studio, I want when i click on button , next activity/fragment should come from right side and present activity sholud gone left.I implimented its working on Activity but not on adapters is showing error.
holder.questions.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(DoctorsProfile.this,Questions.class);
i.putExtra("DOCTOR_ID",doctor_id);
startActivity(i);
overridePendingTransition( R.anim.slide_in_right_up, R.anim.slide_out_right_up);
}
});
overridePendingTransition is working on Activity but not working on Adapters of Recyclerview and Listview, Please tell any other option. I want when i click on recyclerview item next Activity should navigate or come from right side by using overridePendingTransition.
Fragment fragment = Fragment.newInstance();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.fragment_slide_left_enter,
R.anim.fragment_slide_left_exit, R.anim.fragment_slide_right_enter,
R.anim.fragment_slide_right_exit);
Utils.addFragmentToActivity(fragmentTransaction, Fragment, R.id
.content_frame);
This tip features how to change Android’s default animation when switching between Activities.
The code to change the animation between two Activities is very simple: just call the overridePendingTransition() from the current Activity, after starting a new Intent. This method is available from Android version 2.0 (API level 5), and it takes two parameters, that are used to define the enter and exit animations of your current Activity.
Here’s an example:
//Calls a new Activity
startActivity(new Intent(this, NewActivity.class));
//Set the transition -> method available from Android 2.0 and beyond
overridePendingTransition(R.anim.slide_in_right_up, R.anim.slide_out_right_up);
These two parameters are resource IDs for animations defined with XML files (one for each animation). These files have to be placed inside the app’s res/anim folder. Examples of these files can be found at the Android API demo, inside the anim folder.
for example code visit http://www.christianpeeters.com/android-tutorials/tutorial-activity-slide-animation/#more-483
Change like this code you must be passing activity as context in adapter
holder.questions.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(DoctorsProfile.this,Questions.class);
i.putExtra("DOCTOR_ID",doctor_id);
Activity activity = (Activity) context;
activity.startActivity(i);
activity.overridePendingTransition(R.anim.slide_in_right_up, R.anim.slide_out_right_up);
}
});
Note : Context is the Base Object of Activity
Update :
I have checked the accepted answer but hope you understand that it will be called everytime when your activity get launched and thats not supposed to be best practice. I am suggesting better approach if you want to follow the accepted answer .
Alternative :
Pass one parameter in bundle to new activity to make sure that transition coming from that specific adapter so double transation should not happen when you are coming rom any other activity also.
There is a easy way to do this. just put overridePendingTransition on your OnCreate method of next Activity/Fragment.So that when next Activity will come it will come according to your choice.Need not add overridePendingTransition on adapters.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ask_question);
overridePendingTransition( R.anim.slide_in_right_up, R.anim.slide_out_right_up);
}

Android onResume not called after finish() command on different activity

I have 2 Activities. My first activity calls the second activity. Once a button is clicked in the second activity it calls the finish() method on that activity causing it to return to Activity 1. This is fine, but my problem is that when it returns none of the expected methods are called (onResume(), onRestart(), on Start()). Is there anything that I could be missing? Anything that can inhibit the call? I have logs in each of the methods making sure that they aren't called. I have also added the android:noHistory="false" into my manifest to try solving the problem, but it did not fix it.
Here is the way I call the Activity
intent = new Intent(this, ViewEdit.class);
startActivity(intent);
and here is an example of the setup of the log in the methods
public void onStart(Bundle bundle){
super.onStart();
Log.d(MYACT, "On Start");
}
Thanks for any help.

Error in using a checkbox between multiple activities

I am trying to write a settings activity in an application on Eclipse. In the Main Activity, it has a button that runs a certain command. In the settings activity, I want to have a checkbox that when checked, changes what the button in the Main Activity runs when it is tapped. Right now, I have it so that when the checkbox is checked, it changes the value of a boolean and passes it to the main activity. When the button in the main activity is tapped, it checks to see if the boolean is true or false. All of this works perfectly, but when I return to the settings activity after that, the checkbox is unchecked. What should I do to have it stay checked after I go to another activity?
I believe the comment I posted is the answer:
You need to save the state of the activity. This information can be found at Saving Android Activity state using Save Instance State but in short you need to override these two methods:
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
}
and
#Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
}
you can use shared preference in android to store state. take look at this
http://developer.android.com/guide/topics/data/data-storage.html#pref
and
http://www.androidhive.info/2012/08/android-session-management-using-shared-preferences/

android start activity and keep running previous

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

Categories