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

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.

Related

Android Studio: Repoen App with Previous Screen

I'm writing an app with 8 different Activities that are all interconnected(accessible from one another). I'm wondering how I can have the app always reopen from a full close (not just home button click but from closing the background activity) with the same screen it was exited from.
For example, from my Splash Screen I navigate to a home screen. From that home screen I navigate to the Settings. Now, I click the multi-tab viewer and close all apps. How can I have Settings(or wherever I had left off) become the launching activity when the app is reopened?
Thanks in advance!
You can use SharedPreferences to store the class name of the latest Activity. Later, when relaunch, you can redirect to it from the Splash Screen.
You can do that simply by using SharedPreferences. When you launch an activity, it changes it's last activity value in SharedPreferences to point at that activity.
When you close the app and then reopen it, your Main/Launcher activity (Which may be your Splash Screen) then checks for this value and launches the activity according to this value which represents the last activity the app was on.
Here is the code that would fit the Main/Launcher activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String lastActivity = getDefaults("LAST_ACTIVITY", MainActivity.this);
Intent intentLastActivity;
// use an if statement to find out which activity it is
if(!lastActivity.equals("") || !lastActivity.equals(null)){
if(lastActivity.equals("home")){
intentLastActivity = new Intent(MainActivity.this, HomeActivity.class);
}else if(lastActivity.equals("settings")){
intentLastActivity = new Intent(MainActivity.this, SettingsActivity.class);
// add more else-if statements for the other activities.
}
startActivity(intentLastActivity); // launch the last activity
finish(); // finish/close the current activity.
}
}
public static void setDefaults(String key, String value, Context context) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = preferences.edit();
editor.putString(key, value);
editor.apply();
}
public static String getDefaults(String key, Context context) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
return preferences.getString(key, null);
}
Once that code is placed in your Main/Launcher activity. You can then set this last activity value in SharedPreferences in other activities when they are launched.
Code placed in the other activities (in the onCreate method):
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
// we are at the settings activity. Change last activity value to settings
MainActivity.setDefaults("LAST_ACTIVITY","settings",this);
}
Luckily the methods to change the last activity are accessible in other activities.
If you're able to implement this well in your app. It should work as you want it to.
HOPE THIS HELPS!

How to remove activity from backstack in android?

I have an app in which i have three activity namely:- Login,MainActivity and password activity.when I go to password activity and do some event then after login activity comes and here when i press back it will remove login and Mainactivity comes which I don't want. what i want when i press device back twice it will simple close app not to come Mainactivity.How can I archeive this problem.
code that i have tried but not success.
Login code:-
#Override
protected void onResume() {
super.onResume();
clearAllTask();
}
private void clearAllTask() {
CMainActivity m_MainActivity = new CMainActivity();
if (m_MainActivity.m_MainActivity != null) {
m_MainActivity.m_MainActivity.finish();
}
}
and code for MainActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
m_MainActivity = this;
}
You need to clear the back-stack where you are using the Intent
Like this:
Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
Here FirstActivity will get cleared from back-stack and will be finished. Also you will be navigated to SecondActivity. So when you press back button from SecondActivity, it will close the app.
Hope this will solve your problem.

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 start from different Activity in android studio

i make app with two activities.
firs has:
2 ExitText(login and password);
button(confirm, save data with SharedPreferences, intent to second activity).
second one:
2 TextView(get login and password with SharedPreferences);
button(clear data on SharedPreferences, intent to firsActivity).
how to make next: while there are some data on SharedPreferences - app will be started from the 2nd screen.
for example, i made:
if (user!=null && pass!=null){ Intent enterIntent = new Intent(MainActivity.this,SecondActivity.class);
startActivity(enterIntent);
}
but, technically it first run the firstActivity and than go to the secondOne. if there are some method to start app with the another activity (not mainOne)?
You won't be able to check to see if there are values in SharedPreferences before entering one Activity.
What you can do is check the value before displaying the UI (before calling setContentView(R.layout.my_layout)), and either continue along, or start the next Activity.
public class MyStartActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences preferences = getSharedPreferences("my_prefs", Context.MODE_PRIVATE);
if (preferences.contains("my_key")) {
// start next Activity
}
setContentView(R.layout.my_layout);
}
}
If you don't want the first one on the back stack, you can call finish() after starting the second one (or use appropriate flag(s) on intent).
Another approach would be to have only one activity with to fragments and decide dynamically which one set on start. With fragments you can also easily change layouts on button click or on back pressed.
I'm not sure if it will work, because app has to start at the first activity. First activity checks login and pass at share preferences and then you can go to the second activity
try this
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences preference = getSharedPreferences("my_prefs", Context.MODE_PRIVATE);
if (preference.getSting("key",null)!= null) {
// start new Activity
//finish this activity so it not in back stack
} else {
setContentView(R.layout.my_layout);
}
}

How to implement the following?

Given:
6 activities. (A, B, C, D, E, F)
Each activity consists of several edittexts or a camera implementation and a next button to goto next activity (The activity which is going to be opened depends on the value entered by the user).
If edittexts are displayable or not comes from the server.
Incase there are no edittexts on the layout then only next button is shown.
Flow of the application: A->B->C(->D)->E->F
The Activity D opens only when a certain condition is met in activity C.
Todo:
Incase, The activity contains no edittext and only next button i should be able to skip this activity.
If the flow of my application is like: A->B->C->E->F Then when i press hard back the flow should be F->E->C->B->A
If the flow of my application is like: A->B->C->D->E->F Then when i press hard back the flow should be F->E->C->B->A
If the flow of my application is like: A->C->D->E->F Here we skipped B because there are no views in Activity B, When i press hard back the flow should be F->E->C->A
what i did:
To skip the activity when no fields are available inside onCreate(), I am checking if any field is displayable if no then i add the activity name in a stack if it is not present in it and then open the next activity.
// Block for skipping this screen
if (skipScreen) {
Intent i = new Intent(B.this, C.class);
startActivity(i);
finish();
} else {
if (!Constants.st.contains(B.class)) {
Constants.st.push(B.class);
}
}
And when i press back from an activity i pop() the name of that activity from the stack and peek() at the top of the stack and jump to that activity.
public void onBackPressed() {
super.onBackPressed();
Constants.st.pop();
Intent i = null;
if (Constants.st.isEmpty()) {
i = new Intent(B.this, A.class);
} else {
Class<Activity> jumpTo = Constants.st.peek();
i = new Intent(B.this, jumpTo);
}
startActivity(i);
finish();
}
I think the statePattern will be very helpfull. Here is a short tutorial.

Categories

Resources