Cooperation between Activities - android

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)

Related

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.

return to the same activity, When restart the app

I have a lot of activities in my app, and I want that if the user closes the app in activity 13 for example, when opening the app at another time the activity returns in activitty n° 13. how can I do this? thank in advance
You could use SharedPreferences to keep track of the last used activity.
Then you can redirect the user in the onCreate of your main activity to the correct activity, and call finish on your main activity.
This could look something like this:
#Override
protected void onCreate(Bundle savedInstanceState)
(...)
int last_activity = getLastActivityIdFromSharedPreferences();
if (last_activity == 1)
{
this.startActivity(new Intent(this, ActivityOne.class));
finish();
}
(...)
}

onResume() not called second time an Activity is launched

During the normal course of development, I noticed that a particular activity appears to have stopped responding the second time it is called.
i.e. menu->(intent)->activity->(back button)->menu->(intent)
There is nothing relevant in logcat.
I don't even know where to start debugging this nor what code to show so here are the onClick and onResume fragments:
if (!dictionary.getClassName().equals("")) {
this.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i;
i = new Intent(mContext, NVGlobeNavigatorVC.class);
i.putExtra("PAGE_TITLE", title);
i.putExtra("TITLE", _dictionary._title);
mContext.startActivity(i);
});
} else {
findViewById(R.id.greaterthan).setVisibility(View.GONE);
}
and in the Activity being launched:
#Override
protected void onResume() {
super.onResume();
...
Nothing unusual in manifest either:
<activity
android:name=".NVViews.NVGlobeNavigatorVC"
android:theme="#style/WindowTitleBackground"
android:label="GlobeNavigator"/>
For clarity, I put breakpoints on mContext.startActivity(i) and super.onResume(). I click the view with the onClickListener and both breakpoints are hit as expected. I then press the back button which returns me to the menu. onPause() is called as expected.
I touch the view to launch the activity again, and the breakpoint on startActivity is hit but not in onResume() in the target activity. The screen goes black and the only way I can get going again is to restart the app. If I pause the debugger, it pauses in dalvik.system.NativeStart() which implies that the activity is never relaunched.
I don't think it's relevant, but I'm using Intellij IDEA and have deleted all of the output directories, invalidated the caches and done a full rebuild.
Target API is 8. I've tested on 2.3 and 4.0.4.
Any ideas? I'm stumped.
[EDIT]
In onPause, I save some stuff to prefs. The purpose of onResume() is to get them back again:
#Override
protected void onPause() {
super.onPause();
SCPrefs.setGlobeViewViewPoint(globeView.getViewPoint());
SCPrefs.setGlobeViewZoom(globeView.getZoom());
SCPrefs.setGlobeViewScale(globeView.getScale());
}
This code:
i = new Intent(mContext, NVGlobeNavigatorVC.class);
creates a new intent. The intent is of class NVGlobeNavigatorVC.class.
If you call it once, you create a new activity, lets call it "iTheFirst". If you back out of the activity, it executes "on pause". When you run the above code again, you create another new activity of the same class, but a different acitivity. Hence it won't resume your other activity, but make a new one that we could call "iTheSecond". It looks just like iTheFirst but is unique.
If you want to resume the above, after backing out of it, you need to keep a reference to it in your menu. Then in your onClick, look to see if that activity exists, and if not, make a new one and start it, and if it does exist, just resume it.
Here is a sample activity that remembers and restarts an activity:
Intent savedCueCardActivity = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState, R.layout.landing);
}
public void goToNextScreen(View v) {
if (savedCueCardActivity == null) {
savedCueCardActivity = new Intent().setClass(this, CueCardActivity.class);
startActivity(savedCueCardActivity);
// lastActivity.finish();
} else {
savedCueCardActivity.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(savedCueCardActivity);
}
overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
}
I found the problem, and it's a bit esoteric.
I have a large static data structure which is loaded in a class static initialiser. There was a bug in that initialiser causing an infinite loop the second time it was called if the data structure was still loaded.
Because that class is referenced in my Activity, the class loader is loading it before onCreate() or onResume() is called.
The loop gave the appearance that the Activity loader had hung.

Destroy Activity stack

There are some applications in which you can logout at any point within. e.g you login and then your browse around. you use the action bar or the menu button to logout. I can call finish() at that very point but then it will just pop the last activity. Even if i move the user forward to the Home Activity, still the stack remains in memory. Is there any way to destroy the remaining stack?
The easiest way to do this is to clear the stack back to your home or first activity, and pass an identifier saying to exit the app. For example:
public class ActivityOne extends Activity {
public static final String FINISH_THIS = "FINISH_THIS";
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if(intent.hasExtra(FINISH_THIS)) {
finish();
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(getIntent().hasExtra(FINISH_THIS)) {
finish();
}
}
}
public class ActivityTwo extends Activity {
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == R.id.logout) {
Intent intent = new Intent(this, ActivityOne.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra(ActivityOne.FINISH_THIS, true);
startActivity(intent);
}
return true;
}
}
If ActivityOne is the root of your stack and ActivityTwo is where the user selected the option to logout, starting an intent that clears back to ActivityOne will get rid of the backstack. If you want the app to exit when logging out, you can pass an extra like I did with FINISH_THIS to signal the root/home activity to finish.
Another way to do this would be to call setResult(FINISH_THIS) where FINISH_THIS is an int identifier before calling finish(). Then in all other activities in the stack, you'd override onActivityResult and check the result to see if that activity needs to be finished. If it does, you set the result again and keep passing it down the line.
Using the intent method I outlined in the beginning is the preferred method for clearing the stack as it doesn't rely on daisy chaining results together but both options work well enough.
A quick and dirty way to do it would be onResume verify you are still logged in, if not then finish it. That way if they hit back, it will close each activity as it tries to open them. This would also prevent someone from using the app manager to re-enter your activity when you expect it to be closed.
Another idea would be read Android: Clear the back stack

Android MainActivity calls another Activities, after pressing back key, blank screen shows

my main.xml file is just dummy. I want to start different activites based on the condition. If the password is found in shared pref file, the login activity should be launched, and if password is not found, the configuration activity should be launched. it is working fine but when I press the back key from keypad, the main activity is shown (I mean the blank screen because there is nothing) How can I avoid this?
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
prefs = getSharedPreferences(preffilename, MODE_PRIVATE);
final String password = prefs.getString("password",null);
if(password == null)
{
Intent i = new Intent(getApplicationContext(), Configuration.class);
startActivity(i);
}
else
{
Intent i = new Intent(getApplicationContext(), Login.class);
startActivity(i);
}
}
Call finish() from your main activity after calling startActivity(), this will remove main activity from stack.
What is your expectation when you pressed the back button? You might want to put those code in onResume() so it always get called when the main activity is brought back from the stack.

Categories

Resources