I have a main activity that forks a second activity, I wanted to go back from second activity to the main activity without reinitialize it. In other words, I wanted to immediately show the main activity after second activity disappears, instead of re-rendering the first activity. I have tried methods like finish, onBackPressed, none of this goes back directly achieved the effect I desired. Rather it seems that they go back and re-render the activity , which feels like all your previous data that renders the view is lost, and it has to do it from scratch again.
So How can I go back directly without re-rendering again?
Thanks
----------------------Update--------------------
Clase MainActivity extends Activity{
public onCreate(Bundle savedInstanceState){
super.onCreate(SaveInstanceState);
setContentView(R.layout.mainview);
GoogleMap gmap = ...
Route route = ...
//draw a route in the google map
// now the map bears a route on it
Intent intent = new Intent(this, secondActivity.class)
}
}
Class SecondActivity extends Activity{
public onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.secondview);
Button backbut =(Button) findViewById(R.id.backbutton);
}
//backbut is associated with a call back onClick in the xml
private onClick(View){
finish();//after this the second activity disappears, but previously drawn route on google map also disappears, and it takes time to redraw them again. feels like onCreate in firstActivity is reentered again
}
}
You need to use this two flag to accomplish what you want:--
Intent.FLAG_ACTIVITY_CLEAR_TOP
Intent.FLAG_ACTIVITY_SINGLE_TOP
Just use this code:--
Intent intent = new Intent(SecondActivity.this,
MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
you should not call finish() on going second activity then the first activity is running on background and after calling finish() on second activity the first activity appears without re-initialize as privious state.
Hop this will help you.
MainActivity extends Activity{
Context context;
oncreate(){
context = this;
Intent intent = new Intent (context , SecondActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
/* here finish() method is not called, if called MaintActivity objects will destory*/
}
}
Guys thank you all for your kind help! I think I kinda solved my problem. They key is that I made the onClick method private so my xml cannot find my onClick and issues an Exception, which caused activity to destroy and re-render themselves. Hope this explanation makes sense
Related
I have four activities
ListActivity
Itemdetail_Activity
record1_Act
record2_act.
I can go from ListActivity -> record1_Act and from ListActivity <-record1_Act properly but when I want to go from Itemdetail_Activity -> record2_act and return back from Itemdetail_Activity <- record2_act it always goes record2_act to ListActivity.
I even used i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); but it does not work. How to solve this issues?
Here is my code
Intent i = new Intent(record2_act.this, Itemdetail_Activity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(i);
Intent i = new Intent(record1_Act.this, ListActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(i);
finish();
After starting activity remove finish() call because it will clear that activity from stack and i see you have used finish in your code. also you do not need to set these flags.
In activity 4, implement onBackPressed() as,
#Override
public void onBackPressed()
{
Intent i = new Intent(4.this, 3.class);
startActivity(i);
}
call this method if you have any back button. And if you want to link the backpress for every activity you need to implement onBackPressed() method in every activity.
you can call onBackPressed() if previous activity not finished also you can use :
this.finish();
// Or
Activity currentActivity = this;
currentActivity.finish();
and if previous you finished activity you have to track user in activities and make list of activities, then move to :
activitiList.get(activityList.size() - 2);
// activityList.size() - 1 is last activity and activityList.size() - 2 is previous activity
Do check if ItemDetailsActivity is NOT calling finish()
I have an Android Best Practice question. I have to following code, which is working nicely, but I think it is not so elegant. So, my question is: at which point of activity life cycle is nice to start another activity ?
public class LoginActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ParentPreferences parentPreferences = new ParentPreferences(getApplicationContext());
if (parentPreferences.isPassExists()) {
Intent i = new Intent(this, MainActivity.class);
startActivity(i);
} else {
setContentView(R.layout.login);
}
}
}
The task is about: if the parent has already made a password to protect the app, than we don't need to show the LoginActivity. I don't know, is it "healthy" for an Activity to give an intent to launch, when nor the onCreate nor the other lifecycle methods completed.
What are you thoughts guys ?
I think the better way is to create LauncherActivity, and start activitys from them:
For example:
public class LauncherActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ParentPreferences parentPreferences = new ParentPreferences(getApplicationContext());
Intent intent;
if (parentPreferences.isPassExists()) {
intent = new Intent(this, MainActivity.class);
} else {
intent = new Intent(this, LoginActivity.class);
}
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY)
startActivity(i);
}
}
Updated:
Refering to Activity | Android Developer
onCreate is a first lifecycle method, сonsequently better to start activity B when A activity just started and does not inflate any layout
I would have the manifest start your MainActivity or whatever you call it. The MainActivity starts by checking if the user has logged in. If not, it starts your LoginActivity, which comes back in onActivityResult() with the result of the login.
It does depend on the requirement for the user to log in every time they start the app, or just once, or once in a while. If the use has to log in every time, than it's ok to start with LoginActivity. Otherwise, starting LoginActivity every time and passing to MainActivity (or whatever) seems just a waste. "Waste" not in the sense of performance, but of clarity of your app.
I think the best solution for you is to add a SplashScreen or like a "fake" screen.
Here you check if he's logged in already and based on it you start the correct activity.
Maybe the absolutely best way would be to do it with fragments, but you have to change a lot of your app.
About when to call it, the onCreate is perfect :)
I am writing a calculator. There are main 2 modes: polish and reverse polish system.
Anytime MainActivity is called, I like the MainActivity to direct the user to the correct page.
public class MainActivity extends BaseActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Intent intent;
String CalculatorMode = Memory.getPreference("CalculatorMode", this);
if (CalculatorMode.equals("Calc_reverse"))
intent = new Intent(this, Calc_reverse.class);
else
intent = new Intent(this, Calc_normal.class);
startActivity(intent);
}
}
After the user is directed to Calc_normal.class or Calc_reverse.class, and if he pressed the back button, he get back to the MainActivity. But nothing happen! The onCreate() is no more called. How to fix this, and is there a better way to write this? I am trying to learn, and any help is very welcome.
Instead of onCreate() you can call your methods in onStart() which will be called anytime you navigate.
onCreate() won't be called unless the activity is destroyed, you can check the activity lifecycle.
http://developer.android.com/reference/android/app/Activity.html
onStart() activity will be visible to user but won't interact.
I' working on an app with TabGroupActivity.
I'm launching through a tabhost activies so i can have more than one Intents in each tab:
public class MainTabActivity extends TabActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_tab);
final TabHost tabHost = getTabHost();
tabHost.addTab(tabHost.newTabSpec("tab2")
.setIndicator("Que")
.setContent(new Intent(this, TabGroup2Activity.class)));
}
TabGroup2Activity class:
public class TabGroup2Activity extends TabGroupActivity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent i = new Intent(getApplicationContext(),QueActivity.class);
startChildActivity("categorias", i);
}
}
I got the TabGroupActivity from this page:
My issue is when i'm clicking on the second tab, i get my QueActivity.class opened. It's basically a listview with items retrieved from a Data Base. When i clic a row i get a new intent opened with information passed by Bundle object.
The problem is when i hit the back button in this intent, i go back to the QueActivity Intent, which was on onPause() event, but it goes to onStop(), onDestroy() and onStart() event insted going to other state (onResume i think) where there is no need to be created again. The issue is the Intent being created again executes SQL querys and things that i don't need to execute anymore.
I'd like to press the go back and retreive the last intent in a way i don't have to create it again.
I hope I've explained myself succesfully.
Make sure that you have closed the CURSOR off DB in your activity wherever you have used it.
stopManagingCursor(c);
Try this:
<activity ... android:launchMode="singleTop" />
In your manifest file.
This thing is device specific, some devices didnt go to onresume, and start life cycle from start, To handle that you can use Savedinstatnce, to save a state .
How to switch layouts? First, I have a class Main where is onCreate (setContentView(R.layout.main);) and then I call, another class with command:
setContentView(secondClass);
In this class, I draw with Canvas and this work just fine. I also create button to go back in first "class" (R.layout.main), but I don't know how to do it.
Now my program is basic a graph shower. In first class you type your function and them second class draw it. But how to go back in first class to type another function. This "back" button or arrow witch every Android phone have, send me out of program not back on insert part.
In secondClass I can't create onCreate method, but I also tried the following and they didn't work:
Intent abc = new Intent("bla.bla.bla.FIRSTCLASS");
startActivity(abc);
and
Intent abc = new Intent(SecondClass.this,FirstClass.class);
startActivity(greNaPrvoOkno);
If you want to use a custom view (as I understood, you are extending the View class), you can do it in the following way;
Consider you are showing the second class from your Main activity like this;
setContentView(new SecondClass(getApplicationContext(), MainActivity.this));
And you Second class is this (suppose);
// I am using onClickListener to go back to main view. You do whatever you like.
public class SecondClass extends View implements OnClickListener {
// This is needed to switch back to the parent activity
private Activity mParentActivity = null;
public SecondClass(Context context, Activity parentActivity) {
super(context);
mParentActivity = parentActivity;
setOnClickListener(this);
}
#Override
public void onClick(View v) {
// Set the Main view back here.
mParentActivity.setContentView(R.layout.main);
}
}
Disclaimer: This code will do what you have asked for, but may cause other problems.
As advised by #Mudassir, you should use two different activities for two screens. It will give you better control, and your code will be easy to understand and maintain.
On the Onclick event of the button you have to write finish(); that's it..
Both of your classes are Activities yes? IF so then in your second activity you will simply call finish() and your activity will close revealing your first activity again.
When I have used multiple intents in my android application, I have created a new activity through:
Intent abc = new Intent(this, SecondClass.class);
startActivity(abc);
When the button is pressed in your second class, I would then either call finish(); on the class, or create a new intent like so:
Intent abc = new Intent(this, FirstClass.class);
startActivity(abc);
However, this method has the disadvantage that if a user wanted to use the back button, they may have to scroll through many layers of activities.
You should create another activity for your second class but not just set the main activity to a new view setContentView(secondClass).
For an easier modification, You could try to set the view back to setContentView(R.layout.main) first.
You still need to configure the widgets(e.g. TextView) on it when you set it back.
You don't have to startActivity again to go back.
Just call finish() in your second activity when you want to finish the current activity and go back:
e.g. When user press the back button in your second activity
mButtonBack.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
finish();
}
}