How to be directed to page automatically? - android

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.

Related

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

Cooperation between Activities

I have plugged in lock screen logic into my app. It shows ComfirmPatternActivity (above my MainActivity) which controls graphical pin code input to be correct.
When onCreate() method of MainActivity is call everything is OK. But I also want to lock screen when I simply turn app down not destroying MainActivity. So it goes from onRestart() -> onResume(). In onResume() I placed method handleLockScreen(); as in onCreate(). But for now I got into infinite loop hich shows me ComfirmPatternActivity screen for ages. It seamed out that the last command of code in ComfirmPatternActivity after user inputs correct pin - is Activity finish(). After that finish() Im redirected to MainActivity.onResume() - prev Activity on the stack - in which I again start ComfirmPatternActivity() and so on. I want resume logic only in case user pressed on app icon again, not in case top Activity is destroyed. How this can be handled? Thx in advance.
MainActivity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
handleLockScreen();
setContentView(R.layout.activity_main);
}
protected void onResume() {
super.onResume();
handleLockScreen();
..
public void handleLockScreen(){
SharedPreferences prefs = this.getSharedPreferences("LOCK_SCREEN",
Context.MODE_PRIVATE);
String lock_screen_code = prefs.getString("LOCK_SCREEN_CODE","");
if (lock_screen_code.isEmpty()) {
Intent intent = new Intent(this, SampleSetPatternActivity.class);
this.startActivity(intent);
}
else{
Intent intent = new Intent(this, SampleConfirmPatternActivity.class);
this.startActivity(intent);
}
}
public class SampleConfirmPatternActivity extends ConfirmPatternActivity {
SharedPreferences prefs = this.getSharedPreferences("LOCK_SCREEN",
Context.MODE_PRIVATE);
String patternSha1 = prefs.getString("LOCK_SCREEN_CODE","");
return TextUtils.equals(PatternUtils.patternToSha1String(pattern), patternSha1);
... finish() is called further in this activity
}
This finish() returns to my onResume() which triggers all over again. And I want handle onResume() only in case smth external happend to my app like user returned to my app etc. I dont want get back to onResume() when pin code is checked and everything is OK.
You could possibly declare a boolean (global to application) in LockScreen Activity to fix this issue and tell(to the MainActivity) if it is coming to onResume from outside or from LockScreen (y)

How to capture launched finished activity in Android?

I need to do something ( like show an alert ) after my activity launched completely.
Intent intent = new Intent(this, SignInActivity.class);
startActivity(intent);
How to do it?
You'll have to do it in the onCreate of the SignInActivity class as the the first one will go to background and will no longer be able to display stuff on the screen, at least not directly
Whats the problem then!!
You can use the onCreate, onResume of started activity
Intent intent = new Intent(this, SignInActivity.class); startActivity(intent);
after this in SignInActivity use your alert
#Override
protected void onCreate(Bundle bundle)
{
super.onCreate(bundle);
setContentView(R.layout.player);
// Put your alert as the last statement of this method
}
OR
#Override
protected void onResume()
{
// put your alert here
super.onResume();
}
Note: onResume is called every time the activity is resumed
You should take a look at the activity lifecycle document from the official Android docs. As you can see you will receive a callback to one of three methods as your activity launches. If you only want to show it when the activity is first shown you can show the alert dialog in the onCreate call. You could also place it in the onResume call if you would like the dialog to show every time the user leaves this activity and comes back to it. Read through the doc, you'll have a better understanding of how an activity lives inside your application. Below is a quick example of where to place the code.
public class MyActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new AlertDialog.Builder(this)
.setMessage("Hello Android")
.show();
}
}

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

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 .

Notification opens activity, press back button, prevent opening back stack activity?

This looks very similar to my previous question because it's some sort of follow up. I was not very happy with the only solution given; also, the solution was for a problem slightly different from this one. So let me try to explain the problem again...
A notification is created at boot (with a BroadcastReceiver).
My app main activity is opened and the home button is pressed (the activity will be sent to the back stack).
I pull down the status bar and press on the notification previously created at boot.
That will start some activity, different from the main one.
I press the back button and the main activity is displayed.
This is not very different from my previous question... The thing is, "main activity" was just an example. I could have opened the app main activity and then opened the about activity through a menu option and pressed the home button. The back stack would now be MainActivity » AboutActivity. Which means that when the back button is pressed while in "some activity" (started by pressing the notification), we would be brought to the top of the back stack, that is, the about activity.
What basically want is to prevent any other activity to be opened when I press the back button while in "some activity" (again, started by pressing the notification). I want to be brought exactly where I was, that could be the desktop or some other app's activity, but not my app's MainActivity nor AboutAcitivity cause that's not where I was, those were in the back stack, "sleeping" in the background.
I have come up with a solution, but I don't think it's very elegant and I was looking for something more, well, elegant... If you have any other suggestion, please, let me know.
Anyway, this is my proposed solution:
// I use this class for public static (or public static final) members and
// methods
public final class AppHelper {
public static final String KEY_RESUME_FROM_NOTIFICATION = "resumeFromNotification";
private static boolean sResumeFromNotification = false;
public static boolean getResumeFromNotification() {
return sResumeFromNotification;
}
public static void setResumeFromNotification(boolean resumeFromNotification) {
sResumeFromNotification = resumeFromNotification;
}
}
public class MainActivity extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
(...)
}
#Override
protected void onResume() {
super.onResume();
if(AppHelper.getResumeFromNotification()) {
AppHelper.setResumeFromNotification(false);
moveTaskToBack(true);
}
}
}
public class AboutActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
(...)
}
#Override
protected void onResume() {
super.onResume();
if(AppHelper.getResumeFromNotification()) {
AppHelper.setResumeFromNotification(false);
moveTaskToBack(true);
}
}
}
public class SomeActivity extends Activity {
// This will be called when the notification is pressed and the activity is
// not opened yet
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
(...)
extractIntentExtras(intent);
}
// This will be called if the activity is already opened and the
// notification is pressed
#Override
protected void onNewIntent(Intent intent) {
extractIntentExtras(intent);
super.onNewIntent(intent);
}
private void extractIntentExtras(Intent intent) {
Bundle bundleExtras = intent.getExtras();
if(bundleExtras != null) {
// These intent extras are set on the Intent that starts this activity
// when the notification is pressed
AppHelper.setResumeFromNotification(bundleExtras.getBoolean(
AppHelper.KEY_RESUME_FROM_NOTIFICATION));
mRowId = bundleExtras.getLong(AgendaNotesAdapter.KEY_ROW_ID);
populateNoteUpdateFields();
}
}
}
I don't know, but this solution doesn't look very elegant to me (but it works as I expect it) and I'm looking for alternatives or for strong opinions on my proposed solution as an acceptable and good solution. Thoughts?
After doing some more reading perhaps this is the combination of flags you need:
Intent intent = new Intent(mContext, SomeActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
startActivity(intent);
I think that should force your SomeActivity class to be launched in a completely new task.
When launching the Activity from the notification, you can control how the Activity you are about to open is put on the back stack, and what task it's associated with with Intent flags. You can try something like:
Intent intent = new Intent(mContext, SomeActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
If that doesn't work, try setting a few of the other flags until you get the desired behavior.
Do you ever want your MainActivity to stay in history? If not then my simple, crude solution is to finish the MainActivity when it is paused.
(Call this in your MainActivity)
#Override
public void onPause() {
finish();
}
This will ensure that your MainActivity is removed from history when you navigate away from it, and will never appear when the back button is pressed.
This could be used for AboutActivity as well.

Categories

Resources