I have an Android app which I use to register users on my web site. My first task is to register a user if my shared preferences file shows there is no registered user information.
If my app has a registered user, I provide the following code to simply and automatically switch to a "homepage" activity:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.signin);
if( USERPREFERENCES.getString(USERPREFERENCES_USERMAIL, "") == null && USERPREFERENCES.getString(USERPREFERENCES_USERID, "") == null && USERPREFERENCES.getString(USERPREFERENCES_USERNAME, "") == null){
//setContentView(R.layout.signin);
Toast.makeText(SignIn.this, "testing...", Toast.LENGTH_LONG).show();
}else{
Intent intent = new Intent(SignIn.this, Confirmed.class);
startActivity(intent);
}
... other code
So, from my default activity, signin.java, the app will either switch to the Confirmed activity or stay on and display the signin activity.
My problem is, when the system works and I get switched to the the Confirmed activity, I provide a logout onclick listener which is below:
signout.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//USERPREFERENCES.cl
Toast.makeText(Confirmed.this, "signout responding!", Toast.LENGTH_LONG).show();
USERPREFERENCES.edit().clear().commit();
}
});
It responds and clears all my shared preferences variables. But, when I use my menu to manually switch to the sign-in activity, I still get switched back to the Confirmed activity.
This happens even though I can confirm the variables are empty.
This hardly ever will be true:
USERPREFERENCES.getString(USERPREFERENCES_USERMAIL, "") == null
What if you use this instead?
if( USERPREFERENCES.getString(USERPREFERENCES_USERMAIL, null) == null && USERPREFERENCES.getString(USERPREFERENCES_USERID, null) == null && USERPREFERENCES.getString(USERPREFERENCES_USERNAME, null) == null){
//setContentView(R.layout.signin); TRY TO AVOID DOING THIS THING!!!!!
Toast.makeText(SignIn.this, "testing...", Toast.LENGTH_LONG).show();
}else...
Also, as a recommendation... instead of being switching between activities... what if you create just a Signing.java activity and put a ViewFlipper in its layout. That way your app will be not only faster but also easier to maintain.
This is Because When you will switch back to LoginActivity, this will be resumed instead of being created , Means your Login code which you written inOnCreate will not be called because Dthis time Overrider OnResume has been called , not onCreate .
So either write this code again in onResume or call finish() before moving to second activity , so that next time it will call onCreate()
If you navigate back to the first activity, the onCreate is not called again (unless the activity was destroyed for lack of resources). Move the authentication code in onResume.
Related
Our android app is a chat app. Users can paste a branch link in a chat message. When another user taps on it, we want to retrieve the link parameters to take the user to another screen.
Unfortunately, we are unable to retrieve the link parameters when we tap on such link inside the app (note that we are not using a webview), we are getting the error "Warning. Session initialisation already happened.
To force a new session, set intent extra, branch_force_new_session, to true in the onInitFinished(#Nullable JSONObject referringParams, #Nullable BranchError error) method.
How can we solve this? It's not obvious to me how I could pass a new intent param in that use case.
Notes:
Our launcher activity is singleTask
We are on branch.io sdk 4.3.2
onNewIntent() does not seem to be called (in the code below), maybe that is the root cause for our issue.
sample code:
private Branch.BranchReferralInitListener branchReferralInitListener =
new Branch.BranchReferralInitListener() {
#Override
public void onInitFinished(#Nullable JSONObject referringParams, #Nullable BranchError error) {
...
}
#Override
protected void onStart() {
super.onStart();
Branch.getInstance().initSession(branchReferralInitListener, getIntent() != null ?
getIntent().getData() : null, this);
}
#Override
public void onNewIntent(Intent intent) {
super.onNewIntent(intent);
this.setIntent(intent);
// activity will skip onStart, handle this case with reInitSession
Branch.getInstance().reInitSession(this, branchReferralInitListener);
}
This is a known issue with the Android SDK v4.3.2 and we are working on a resolution.
Would suggest you to try the following in the meanwhile:
When the user tries to open an app that is running in the background, we get an error for set branch_force_new_session to true.
Branch SDK gets initialised on onStart for the Launcher Activity and when the app comes foreground from the background, its on onResume.
In this scenario, we could need to re-initialise the SDK here.
Would request you to implement the below snippet as per Branch docs (https://docs.branch.io/apps/android/#initialize-branch)
// activity will skip onStart, handle this case with reInitSession
Branch.getInstance().reInitSession(this, branchReferralInitListener);
Alternatively, would suggest you to install Branch SDK v4.3.1.
Initialized, you branch IO in application class so that it will initialize once and will not be require again
// Branch logging for debugging
Branch.enableLogging();
//Disable Device ID #2966
Branch.disableDeviceIDFetch(true);
// Initialize the Branch object
BranchIOManager.setupBranchInstance(this);
// It tells the Branch initialization to wait for the Google Play Referrer before proceeding.
Branch.enablePlayStoreReferrer(1000L);
Then inside initSession() branch method use. Pass them as JSON Object to method where you can retrieve the value based on key names.
if (branch != null && uri != null) {
branch.initSession(new Branch.BranchUniversalReferralInitListener() {
#Override
public void onInitFinished(BranchUniversalObject branchUniversalObject, LinkProperties linkProperties, BranchError error) {
// Log.d("onInitFinished", error + "");
if (error == null && branchUniversalObject != null) {
JSONObject jBranch = branchUniversalObject.getContentMetadata().convertToJson();
if (!branchJSONString.equals(jBranch.toString())) {
//This check is applied as if we launch another mLandingScreenPhoneActivity from Branch link then app will become in loop
branchJSONString = jBranch.toString();
loadScreenFromBranchIODynamicLink(jBranch, 0);
}
}
if (error != null) {
// //Toast.makeText(LandingScreenPhoneActivity.this, error + "", Toast.LENGTH_SHORT).show();
}
}
}, uri, this);
}
Here you can get screen values
String screenName = referringParams.optString("screen_key");
//Screen in app where needs to navigate
int screenIndex = referringParams.optInt("screen_index");
the problem: if user doesn't upload any image, app crashes, as cannot store previous image...
Overview: my application consists of Category and Editor activities.
Editor activity has a button to upload image and save the activity.
Category activity displays image from Editor activity.
Problem here --> once the user returns back to edit details in Editor activity, app crashes with below error:
Attempt to invoke virtual method 'java.lang.String android.net.Uri.toString()' on a null object reference
If user uploads an image again - same or different, doesn't matter, the app works well.
The goal is to keep the image if the user doesn't want to upload a new one, just like the app keeps the values for EditText fields:
private void saveInventory() {
// Read from input fields
// Use trim to eliminate leading or trailing white space
String nameString = mNameEditText.getText().toString().trim();
String infoString = mAdditionalInfoEditText.getText().toString().trim();
String priceString = mPriceEditText.getText().toString().trim();
String quantityString = mQuantityTextView.getText().toString().trim();
String image = actualUri.toString(); <---- error here
Image is selected in this method:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent resultData) {
// The ACTION_OPEN_DOCUMENT intent was sent with the request code READ_REQUEST_CODE.
// If the request code seen here doesn't match, it's the response to some other intent,
// and the below code shouldn't run at all.
if (requestCode == SELECT_AN_IMAGE_REQUEST && resultCode == Activity.RESULT_OK) {
// The document selected by the user won't be returned in the intent.
// Instead, a URI to that document will be contained in the return intent
// provided to this method as a parameter. Pull that uri using "resultData.getData()"
if (resultData != null) {
actualUri = resultData.getData();
mPhotoImageView.setImageURI(actualUri);
}
}
}
I believe that saving the activity state and restoring it might solve the problem (code below), but I don't know where to paste it in my code....
// Save the activity state when it's going to stop.
// #Override
// protected void onSaveInstanceState(Bundle outState) {
// super.onSaveInstanceState(outState);
// outState.putParcelable("actualUri", actualUri);
// }
// Recover the saved state when the activity is recreated.
// #Override
// protected void onRestoreInstanceState(#NonNull Bundle savedInstanceState) {
// super.onRestoreInstanceState(savedInstanceState);
// actualUri= savedInstanceState.getParcelable("actualUri");
// }
Probably someone knows how to keep same image upon saving the editor activity. Thank you.
P.S. The finish activity quits the editor, and returns to Catalog. All data is saved in Database - apart from image.
#Override public boolean onOptionsItemSelected(MenuItem item) {
// User clicked on a menu option in the app bar overflow menu switch (item.getItemId())
{ // Respond to a click on the "Save" menu option case R.id.action_save:
// Save to database saveInventory();
// Exit activity finish(); return true;
I have solved the isssue.
I had to remove the line that causes an error in 'Save' class, and create a separate validation in this class:
if (actualUri== null) {
Toast.makeText(this, getString(R.string.image_required), Toast.LENGTH_SHORT).show();
return hasAllRequiredValues;
} else {
values.put(InventoryEntry.COLUMN_INVENTORY_IMAGE, actualUri.toString());
}
In my android app I want to change the input method. So I start a new Activity which shows the language settings in the device. Then user can change it. However then I want to know that if the user has changed it. So I wrote a function for that also. My code so far is...
Intent enableIME = new Intent(android.provider.Settings.ACTION_INPUT_METHOD_SETTINGS);
startActivityForResult(enableIME,0);
if(isInputMethodEnabled()){
activateshadow.setBackgroundDrawable(getResources().getDrawable(R.drawable.button_pressed));
activateshadow.setText("Deactivate Shadow");
prefs.edit().putBoolean("Activate", false).commit();
}else{
Toast.makeText(MainActivity.this,"You haven't change the input method to simpleIME.In order to activate you must change it.",Toast.LENGTH_SHORT).show();
}
my is inputMethodEnabled function is....
public boolean isInputMethodEnabled() {
boolean isIME ;
String id = Settings.Secure.getString(getApplicationContext().getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
String [] name = id.split("/.");
// Toast.makeText(MainActivity.this,"s:"+name[1]+":s",Toast.LENGTH_SHORT).show();
if(name[1].contains("SimpleIME") ){
isIME = true ;
}else{
isIME = false;
}
// Toast.makeText(MainActivity.this,"Returning..."+isIME,Toast.LENGTH_SHORT).show();
return isIME;
}
if(isInputMethodEnabled()) always fails because when the new intent(settings) opens and it take some time to change the input method to simpleIME . How to fix this problem?
You catch when a launched Activity returns in onActivityResult. The requestCode you supplied to startActivityForResult will be a parameter, as will the Activity's result. The Activity may also set other data which you didn't ask about.
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 555) {//555 is the intent ID you gave in startActivityForResult(enableIME,555);
if (resultCode == /*Result1*/)
//Do something
else {
//Do something else
}
}
}
You need a unique id when calling startActivityForResult(enableIME,0);
startActivityForResult(enableIME, 555);
Better still replace 555 with a named variable.
if u look at android life cycle, when activity is finished whole android call onDestroy() method.
so u can call and override this method.
just need write:
#override
protected void onDestroy(){
// code
super.onDestroy();
}
u can manage and override all of life cycle's parts in android
e.g: onResume to get current activity
i hope this help u
In my application I have used some code from the iosched 2012 app. In specific the starting workflow is the following:
1.The user presses the launcher icon of the app
2.HomeActivity checks if the user is authenticated. If he/she is not, it starts the Authentication activity, passing it intent to it and finishes itself
3.When the login process is successful, the authenction activity starts an activity in order to start the HomeActivity and finishes itself
4.HomeActivity checks again if the user is authenticated and displays the home screen of the application.
The following code works like a charm in API Level > 11. Today, I tried the app in a Gingerbread and it fails. Step 3 works, but although the HomeActivity starts it's not brought to front. You have to use the recent list and choose the application in order to see the homeactivity and its now displayed content.
Here's the code and check from the HomeActivity in the oncCreate method
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(!AccountUtils.isSystemAuthenticated(this)) {
AccountUtils.startSystemAuthentication(this, getIntent());
finish();
} else if(!AccountUtils.isAppAuthenticated(this)) {
AccountUtils.startAppAuthentication(this, getIntent());
finish();
}
if(isFinishing()) {
return;
}
setContentView(R.layout.activity_main);
...
}
}
The method invoked in the Authentication activity after the login process is completed
protected void handleLoginSuccess(LoginServiceResponse response, String username, String password) {
if(....) {
if(mFinishIntent != null) {
mFinishIntent.addCategory(Intent.CATEGORY_LAUNCHER);
mFinishIntent.setAction(Intent.ACTION_MAIN);
mFinishIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(mFinishIntent);
}
finish();
} else {
super.handleLoginSuccess(response, username, password);
}
}
Where the mFinishIntent member variable is the intent passed from the HomeActivity (using getIntent())
As I mentioned, in API Level > 11, this works well, and the breakpoint in HomeActivity's onCreted method is hit twice, while in a Gingerbread phone, is hit only once (only when the application starts).
Do I have to use another flag or do you have any other idea of what's going on?
Thanks
What is probably happening is that the activity is only created when the app is started and then when you go back to it from the Authentication activity, it is simply resumed. Try putting the authentication checking code in HomeActivity in the onResume() method.
Here is some more info: http://developer.android.com/reference/android/app/Activity.html#ProcessLifecycle
I have a strange scenario here.
I have this code:
// For checking if the person is logged in.
first_time_check();
setContentView(R.layout.main);
// ...next lines of code
and the first_time_check() function checks if the user is logged in for the first time. If their user_id is not in the SharedPreferences, I redirect them to log in:
public void first_time_check()
{
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences( ProblemioActivity.this);
String user_id = prefs.getString( "user_id", null ); // First arg is name and second is if not found.
String first_time_cookie = prefs.getString( "first_time_cookie" , null );
// About setting cookie. Check for first time cookie
if ( first_time_cookie == null )
{
// This user is a first-time visitor, 1) Create a cookie for them
first_time_cookie = "1";
// 2) Set it in the application session.
prefs.edit().putString("first_time_cookie", first_time_cookie ).commit();
// Make a remote call to the database to increment downloads number
// AND send me an email that it was a new user.
}
else
{
// If not first time, get their id.
// If user_id is empty, make them an account.
// If id not empty, call the update login date and be done.
if ( user_id == null )
{
// User id is null so they must have logged out.
Intent myIntent = new Intent(ProblemioActivity.this, LoginActivity.class);
ProblemioActivity.this.startActivity(myIntent);
}
else
{
// Make a remote call to the database to increment downloads number
}
}
return;
}
So after the code executes the
Intent myIntent = new Intent(ProblemioActivity.this, LoginActivity.class);
ProblemioActivity.this.startActivity(myIntent);
it still executes below the original code that calls this functions.
Any idea how that can happen?
Thanks!!
This is excerpted from the Dev Guide
Shutting Down an Activity
You can shut down an activity by calling its finish() method.
You can also shut down a separate activity that you previously
started by calling finishActivity().
Note: In most cases, you should not explicitly finish an activity
using these methods. As discussed in the following section about the
activity lifecycle, the Android system manages the life of an
activity for you, so you do not need to finish your own activities.
Calling these methods could adversely affect the expected user
experience and should only be used when you absolutely do not want
the user to return to this instance of the activity.
Calling finish() on the activity seems appropriate here as you do not want the user to return to this activity.