Pressing back button stops and destroys my Activity insted of resuming it - android

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 .

Related

Navigate Between Activities (Android Studio)

I got a problem when I navigate between two activities, it shows me error and I don't know what is the problem. I am very sure that my code is correct, because it just simple Intent navigate by on click Button.
When I Press the button to go to the next activity it returns me to the fist activity (not the desire one). Note that both activity has background image.
Fist Activity
public class firstActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first_activity);//has a background img and one button
}
public void nextPage(View view){
Intent StartNewActivity = new Intent(firstActivity.this, secondActivity.class);
startActivity(StartNewActivity);
overridePendingTransition(R.layout.slide_in_up, R.layout.slide_out_up);
}
}
Second Activity
public class secondActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_activity);// has a background img and one button
}
public void nextPage(View view){
Intent StartNewActivity = new Intent(secondActivity.this, thirdActivity.class);
startActivity(StartNewActivity);
overridePendingTransition(R.layout.slide_in_up, R.layout.slide_out_up);
}
}
This is the error message
Could not find class 'android.graphics.drawable.RippleDrawable', referenced from method android.support.v7.widget.AppCompatImageHelper.hasOverlappingRendering
Also, I did not use any ripple drawable in my app.Even though I don't know what does it mean?
Thanks,
Something you have wants to to find a refwrence to that ripple component, you need to find out what.
Otherwise, you can try to make sure you have added a reference to the support.v7.widget in the second activity and see if the exception goes away.
Aside from that, we would need to see more code to help further.
When I Press the button to go to the next activity it returns me to the fist activity (not the desire one)
It means that your app crashes when loading your new activity, so it comes back the first one.
Check your activity layout, style configuration => clean your project => Run again.
Hope it can help.
I solved my problem by resizing the background images of the activities, and I added this extra attribute in the manifests file
<application
android:largeHeap="true" >
</application>

When good time to start another activity

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

How to be directed to page automatically?

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.

how to go back to previous activity without re-rendering it

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

Android tabactivity problem

I'm having some difficulties getting the TabActivity to work. Here's the implementation of the class:
public class Profile extends TabActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final TabHost tabHost = getTabHost();
if (tabHost.isEnabled()) {
Log.e("profile", "enabled");
}
tabHost.addTab(tabHost.newTabSpec(getString(R.string.friendReqs))
.setIndicator("requests")
.setContent(new Intent(this, FriendRequests.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
tabHost.addTab(tabHost.newTabSpec(getString(R.string.friends))
.setIndicator("photo list")
.setContent(new Intent(this, Settings.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
Log.e("profile", "add tabs");
tabHost.invalidate();
}
}
The problem is that I call this class from my main activity (which is a MapActivity if it matters) and when I do the TabActivity doesn't show. It registers the click on the option menu and it even starts the intent but the screen doesn't change..it just stays on the main activity and i see in the logs that the main activity gets resumed. I call it like this:
Intent p = new Intent(this,Profile.class);
p.putExtra(DBAdapter.KEY_USERID,userid);
startActivity(p);
Like I said...there are no errors (the classes called from the tabs exist of course), just no actions. I put some log commands into the onCreate function in the tabactivity (as you see) and they all get written into the log...I have no idea what I'm doing wrong here. Any help?
After some tries...
I set the first tab to a view instead of an intent and it showed the tabs...i could also select the 2nd tab.
Got it. The problem was the Activity being called in the 1st tab. It closed if there was no data passed to it (bundle). Thanks again for the help.
Remove
tabHost.invalidate();
Also you can drop
Intent.FLAG_ACTIVITY_CLEAR_TOP
I don't see any purpose of this, as you need to see the activity anyway when you click on tabs.
I don't see setContentView() in your Activity, which you need.

Categories

Resources