I have two activities, LoginActivity and MainActivity.
LoginActiviy is the launcher Activity, its purpose is to check whether the user is signed in or not if he's signed in; go to MainActivity.
Although I set android:noHistory="true" to LoginActivity the activity's onResume(LoginActivity) is called again when user exits(means onPause called) the program and launch it again.
Did I misunderstood what noHistory means ? if so what can I do to make the OS forget about the existence of LoginActivity?
EDIT : I tried to put this on LoginActivity's onResume , but it calls MainActivity's onCreate, which I don't want
if(!firstTime) {
goToMainActivity();
}
LoginActivity :
public class LoginActivity extends Activity {
protected static final String PASSED_TWITTER = "mosaed.thukair.alsafytooth.LoginActivity";
private static final String TAG = "mosaed.thukair.alsafytooth.LoginActivity";
protected static final int RESULT_BROWSER = 0;
private SharedPreferences prefs;
private Twitter twitter;
private RequestToken requestToken;
private AccessToken accessToken;
private String authUrl;
private Button login;
private boolean firstTime;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.prefs = PreferenceManager.getDefaultSharedPreferences(this);
firstTime = true;
if(isAuthenticated()) {
Log.i(TAG, "splash screen");
setContentView(R.layout.splash_screen);
String token = prefs.getString(Constants.OAUTH_TOKEN, "");
String tokenSecret = prefs.getString(Constants.OAUTH_TOKEN_SECRET, "");
Log.i(TAG, "oauth login");
OAuthLogin(token, tokenSecret);
} else {
setContentView(R.layout.activity_login);
login = (Button) findViewById(R.id.connect_button);
login.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Log.i(TAG, "clicked");
LoginActivity.this.setContentView(R.layout.splash_screen);
OAuthLogin();
}
});
}
}
private boolean isAuthenticated() {
String token = prefs.getString(Constants.OAUTH_TOKEN, "");
if(token.equals(""))
return false;
String secret = prefs.getString(Constants.OAUTH_TOKEN_SECRET, "");
if(secret.equals(""))
return false;
return true;
}
private void OAuthLogin() {
twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer(Constants.CONSUMER_KEY, Constants.CONSUMER_SECRET);
new AsyncTask<Void,Void,Void>() {
#Override
protected Void doInBackground(Void... params) {
try {
requestToken = twitter.getOAuthRequestToken(Constants.CALLBACK_URL);
authUrl = requestToken.getAuthenticationURL();
Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(authUrl));
myIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP |
Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_FROM_BACKGROUND);
Log.i(TAG, "open browser");
LoginActivity.this.startActivity(myIntent);
} catch (TwitterException e) {
e.printStackTrace();
}
return null;
}
}.execute();
}
private void OAuthLogin(final String token, final String tokenSecret) {
twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer(Constants.CONSUMER_KEY, Constants.CONSUMER_SECRET);
new AsyncTask<Void,Void,Void>() {
#Override
protected Void doInBackground(Void... params) {
AccessToken accessToken = new AccessToken(token, tokenSecret);
twitter.setOAuthAccessToken(accessToken);
return null;
}
#Override
protected void onPostExecute(Void param) {
goToMainActivity(twitter);
}
}.execute();
}
#Override
protected void onResume() {
super.onResume();
Log.i(TAG, "onResume");
if ((this.getIntent() != null) && (this.getIntent().getData() != null)) {
setContentView(R.layout.splash_screen);
new AsyncTask<Void,Void,Void>() {
#Override
protected Void doInBackground(Void... params) {
Uri uri = LoginActivity.this.getIntent().getData();
afterBrowser(uri);
return null;
}
#Override
protected void onPostExecute(Void uri) {
storeAccessToken();
goToMainActivity(twitter);
}
}.execute();
} else if(!firstTime) {
goToMainActivity(twitter);
}
}
private void afterBrowser(Uri uri) {
String verifier = uri.getQueryParameter("oauth_verifier");
String token = uri.getQueryParameter("oauth_token");
try {
twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer(Constants.CONSUMER_KEY, Constants.CONSUMER_SECRET);
requestToken = new RequestToken(token, Constants.CONSUMER_SECRET);
accessToken = twitter.getOAuthAccessToken(requestToken,
verifier);
twitter.setOAuthAccessToken(accessToken);
} catch (TwitterException ex) {
Log.e(TAG, "" + ex.getMessage());
}
}
private void storeAccessToken() {
prefs.edit()
.putString(Constants.OAUTH_TOKEN, accessToken.getToken())
.putString(Constants.OAUTH_TOKEN_SECRET, accessToken.getTokenSecret())
.commit();
}
private void goToMainActivity(Twitter twitter) {
firstTime = false;
Intent myIntent = new Intent(this, MainActivity.class);
MyApplication.getInstance().setTwitter(twitter);
startActivity(myIntent);
}
}
if(!firstTime) {
goToMainActivity();
finish();
}
What no history does is that it doesn't let that certain activity register in the stack of past activities, it doesn't allow it to skip parts of the Activity lifecycle.
If you don't want certain code not to execute then you should do something like:
Login Activity:
if(!firstTime) {
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
intent. putExtra("skip", true);
finish();
}
Main Activity: (inside onCreate)
if(!getIntent().getBundle().getBoolean("skip", false)) {
//You code that you don't want
}
This is the activity lifecycle I hope it's beneficial to you:
android:noHistory Whether or not the activity should be removed from
the activity stack and finished (its finish() method called) when the
user navigates away from it and it's no longer visible on screen —
"true" if it should be finished, and "false" if not. The default value
is "false". A value of "true" means that the activity will not leave a
historical trace. It will not remain in the activity stack for the
task, so the user will not be able to return to it.
This attribute was introduced in API Level 3.
Quoting the documentation, "it's finish() method called", have you tried finishing the activity yourself?
noHistory = true means once the activity is finish() for that user session, the user will never see it again, however, if the activity is just being paused without finishing, then it will be restarted when going back to it. Before you go to the main activity, just finish() it, if thats your desired behavior.
Related
Ok I have a togglebutton in my main activity. I want the state of this button to be saved if I switch to another activity, minimize the app and a service should be able to set the button state to false (not clicked). Everything worked fine, but for some reason when I started Android Studio again it didn´t work anymore?!
MainActivity:
monitor = (ToggleButton)findViewById(R.id.toggleButton);
monitor.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (monitor.isChecked()) {
Intent intent = new Intent(MainActivity.this, NotifiyService.class);
startService(intent);
} else {
Intent intent = new Intent(MainActivity.this, NotifiyService.class);
stopService(intent);
}
}
});
In the onStop Method the following is executed:
if (monitor.isChecked())
{
tbstate = true;
SharedPreferences sharedPreferences1 = getSharedPreferences("tbstate",MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences1.edit();
editor.putBoolean("keyTB",tbstate);
editor.commit();
}
else
{
tbstate = false;
SharedPreferences sharedPreferences1 = getSharedPreferences("tbstate",MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences1.edit();
editor.putBoolean("keyTB",tbstate);
editor.commit();
}
The onStart Method:
//get Togglebutton state
SharedPreferences sharedPreferences6 = getSharedPreferences("tbstate", MODE_PRIVATE);
monitor.setChecked(sharedPreferences6.getBoolean("keyTB",false));
Asynctask (not complete). In the onPostExecute I set the tbstate to false and in onDestroy it is saved in the sharedpreference. Then a message pop ups which "leads" to main2activity of the same app. When I go from main2activity to mainactivity the togglebutton is still activated.
I hope it is clear what I want ;-)
#Override
public void onDestroy() {
//super.onDestroy();
Th1.interrupt();
checkhttp.cancel(false);
SharedPreferences sharedPreferences8 = getSharedPreferences("tbstate",MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences8.edit();
editor.putBoolean("keyTB", tbstate);
Toast.makeText(NotifiyService.this,getResources().getString(R.string.MonStopped), Toast.LENGTH_LONG).show();
stopSelf();
}
#Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
return null;
}
private static class HttpTaskParams{
boolean value;
String address;
HttpTaskParams(boolean value, String address){
this.value = value;
this.address = address;
}
}
private class HttpTask extends AsyncTask<HttpTaskParams,Void,Boolean>{
#Override
protected Boolean doInBackground(HttpTaskParams... params) {
boolean value = params[0].value;
String address = params[0].address;
try {
URL url = new URL(address);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("HEAD");
httpURLConnection.setConnectTimeout(3000);
httpURLConnection.setReadTimeout(3000);
httpURLConnection.connect();
value = true;
return value;
} catch (MalformedURLException e) {
e.printStackTrace();
value = false;
return value;
} catch (IOException e) {
e.printStackTrace();
value = false;
return value;
}
}
#Override
protected void onPostExecute(Boolean result) {
if(result){
//Notification in Status Bar
NotificationCompat.Builder builder = new NotificationCompat.Builder(NotifiyService.this);
builder.setSmallIcon(R.drawable.dummy);
Intent intent = new Intent(NotifiyService.this, Main2Activity.class);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(NotifiyService.this,0,intent,0);
builder.setContentIntent(pendingIntent);
builder.setLights(Color.YELLOW, 600, 600);
builder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.dummy));
builder.setContentTitle(getResources().getString(R.string.newNotify));
builder.setContentText(getResources().getString(R.string.newNotify2));
builder.setAutoCancel(true);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(1, builder.build());
tbstate=false;
onDestroy();
}
}
}
You need call commit() method of shared preference editor after any change in shared preference..
You haven't called this in ondestroy method
It happens because onPostExecute methods executes after onDestroy of Activity and preferences didn't saved. You need to save preferences in onPostExecute method too
i am trying to implement below code every thing works fine how ever
here is the situation i am facing
mainactivity just has a button to open splashactivity
splash activity does the parsing part then opens the listactvity
my workflow is if user has clicked on the button in main activity
it opens the splash activity and redirects to listactivty the
problem occurs when on the splash activity if a user clicks the
andorid back button it does go back to the mainactivity however the
parsing in splashactivity continues and user is redirected to
listactivty this thing should not happen, when user on splash
activity has clicked backbutton it should go back to mainactivity
and stay there itself
how can this be done
package com.site.name;
public class SplashActivity extends Activity {
RSSFeed feed;
String fileName;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
fileName = "TDRSSFeed.td";
Intent i = getIntent();
int position = i.getExtras().getInt("position");
String[] country = i.getStringArrayExtra("country");
// //public String RSSFEEDURL = "http://blogname.blogspot.com//feeds/posts/default/-/Awards?alt=rss";
Toast.makeText(getApplicationContext(), country[position], Toast.LENGTH_SHORT).show();
//Toast.makeText(getApplicationContext(), country[position], Toast.LENGTH_SHORT).show();
File feedFile = getBaseContext().getFileStreamPath(fileName);
ConnectivityManager conMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
if (conMgr.getActiveNetworkInfo() == null) {
// No connectivity. Check if feed File exists
if (!feedFile.exists()) {
// No connectivity & Feed file doesn't exist: Show alert to exit
// & check for connectivity
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(
"Unable to reach server, \nPlease check your connectivity.")
.setTitle("TD RSS Reader")
.setCancelable(false)
.setPositiveButton("Exit",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int id) {
finish();
}
});
AlertDialog alert = builder.create();
alert.show();
} else {
// No connectivty and file exists: Read feed from the File
Toast toast = Toast.makeText(this,
"No connectivity!",
Toast.LENGTH_LONG);
toast.show();
//feed = ReadFeed(fileName);
startLisActivity(feed);
}
} else {
// Connected - Start parsing
new AsyncLoadXMLFeed().execute();
}
}
private void startLisActivity(RSSFeed feed) {
Bundle bundle = new Bundle();
bundle.putSerializable("feed", feed);
// launch List activity
Intent intent = new Intent(SplashActivity.this, ListActivity.class);
intent.putExtras(bundle);
startActivity(intent);
overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
// kill this activity
finish();
}
private class AsyncLoadXMLFeed extends AsyncTask<Void, Void, Void> {
#Override
protected Void doInBackground(Void... params) {
// Obtain feed
DOMParser myParser = new DOMParser();
Intent i = getIntent();
int position = i.getExtras().getInt("position");
String[] country = i.getStringArrayExtra("country");
//feed = myParser.parseXml(RSSFEEDURL);
//feed = myParser.parseXml("http://blogname.blogspot.com//feeds/posts/default/-/Awards?alt=rss");
feed = myParser.parseXml("http://blogname.blogspot.com//feeds/posts/default/-/" + country[position] + "?alt=rss");
if (feed != null && feed.getItemCount() > 0)
WriteFeed(feed);
return null;
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
startLisActivity(feed);
}
}
// Method to write the feed to the File
private void WriteFeed(RSSFeed data) {
FileOutputStream fOut = null;
ObjectOutputStream osw = null;
try {
fOut = openFileOutput(fileName, MODE_PRIVATE);
osw = new ObjectOutputStream(fOut);
osw.writeObject(data);
osw.flush();
}
catch (Exception e) {
e.printStackTrace();
}
finally {
try {
fOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
#Override
public void onBackPressed() {
//Include the code here
return;
}
}
You can cancel parsing the feed in MainActivity's onRestart() as this is one method of MainActivity that will be called when you press back from your Parsing Activity.
You can read more about Activity Life Cycle here.
For detecting the Back button press event use the following method
#Override
public void onBackPressed() {
// do something on back.
// Change your activity by calling intent
return;
}
If you want to do anything with your back button then you have to override it manually.
#Override
Public void onBackPressed() {
//do whatever you want to do as your question is quite confusing.
return;
}
I am trying to create Twitter login for my Android app. Once I log in, I change the twitter btn title from "Connect with Twitter" to "Disconnect Twitter" and I write out my Twitter username. The only strange thing is that if now I click BACK to quit the application, I go back to where I was before, meaning the button says "Connect with Twitter" againand my Twitter username is not displayed. I am logged in, because if now I click BACK again, I quit the app (go back to the Twitter auth page first) and when I open the app again, the session lives, and the button says "Disconnect Twitter".
Why?
public class Activity_Splash extends Activity {
SharedPreferences sharedPreferences;
private static Twitter twitter;
private static RequestToken requestToken;
private static SharedPreferences mSharedPreferences;
private AccessToken accessToken;
User user;
String twitter_username = "";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
mSharedPreferences = getSharedPreferences(TwitterConst.PREFERENCE_NAME, MODE_PRIVATE);
if (!isTwitterLoggedInAlready()) {
Log.i("Splash start", "Twitter is NOT connected");
Uri uri = getIntent().getData();
if (uri != null && uri.toString().startsWith(TwitterConst.CALLBACK_URL)) {
final String verifier = uri.getQueryParameter(TwitterConst.IEXTRA_OAUTH_VERIFIER);
new Connect_Twitter().execute(verifier);
} else {
Log.i("Splash start", "Twitter is connected");
}
if (isTwitterLoggedInAlready()) {
btn_signupTW.setText("Disconnect Twitter");
btn_signupTW.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
disconnectTwitter();
}
});
} else {
btn_signupTW.setText("Connect with Tw");
btn_signupTW.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
connectTwitter();
}
});
}
private boolean isTwitterLoggedInAlready() {
return mSharedPreferences.getString(TwitterConst.PREF_KEY_TOKEN, null) != null;
}
private void connectTwitter() {
Log.i("Twitter", "connectTwitter()");
ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
configurationBuilder.setOAuthConsumerKey(TwitterConst.CONSUMER_KEY);
configurationBuilder.setOAuthConsumerSecret(TwitterConst.CONSUMER_SECRET);
Configuration configuration = configurationBuilder.build();
twitter = new TwitterFactory(configuration).getInstance();
Thread thread = new Thread(new Runnable(){
#Override
public void run() {
try {
requestToken = twitter.getOAuthRequestToken(TwitterConst.CALLBACK_URL);
Log.i("Twitter connectTwitter", "Please authorize this app!");
Activity_Splash.this.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(requestToken.getAuthenticationURL())));
} catch (TwitterException e) {
e.printStackTrace();
Log.i("Twitter connectTwitter", e.getMessage() + "");
}
}
});
thread.start();
}
private void disconnectTwitter() {
Log.i("Twitter", "disconnectTwitter()");
SharedPreferences.Editor e = mSharedPreferences.edit();
e.remove(TwitterConst.PREF_KEY_TOKEN);
e.remove(TwitterConst.PREF_KEY_SECRET);
e.commit();
btn_signupTW.setText("Connect with Tw");
tv_slogan.setText("Log in with Facebook");
}
public class Connect_Twitter extends AsyncTask {
#Override
protected void onPostExecute(String result) {
Log.i("SIKER", result + "");
tv_slogan.setText(result);
btn_signupTW.setText("Disconnect Twitter");
}
#Override
protected void onPreExecute() {
}
#Override
protected String doInBackground(String... params) {
try {
accessToken = twitter.getOAuthAccessToken(requestToken, params[0]);
Log.e("Twitter OAuth Token1", "> " + accessToken.getToken());
Editor e = mSharedPreferences.edit();
e.putString(TwitterConst.PREF_KEY_TOKEN, accessToken.getToken());
e.putString(TwitterConst.PREF_KEY_SECRET, accessToken.getTokenSecret());
e.commit();
long userID = accessToken.getUserId();
Log.i("Twitter userID", String.valueOf(userID) + "");
User user = twitter.showUser(userID);
twitter_username = user.getName();
} catch (Exception e) {
e.printStackTrace();
}
return twitter_username;
}
}
}
Twitter just launched Fabric, a set of SDKs with several services to mobile developers. One of them is Twitter kit, it provides Sign in with Twitter, Tweet composing, Embedded tweets and API calls directly from the SDK.
Instead of trying to implement this OAuth flow in your app, Sign in with Twitter with this SDK can be integrated this way:
private void setUpTwitterButton() {
twitterButton = (TwitterLoginButton) findViewById(R.id.twitter_button);
twitterButton.setCallback(new Callback<TwitterSession>() {
#Override
public void success(Result<TwitterSession> result) {
SessionRecorder.recordSessionActive("Login: twitter account active", result.data);
startThemeChooser();
}
#Override
public void failure(TwitterException exception) {
Toast.makeText(getApplicationContext(),
getResources().getString(R.string.toast_twitter_signin_fail),
Toast.LENGTH_SHORT).show();
Crashlytics.logException(exception);
}
});
}
You can find more details about that at:
http://t.co/fabric
https://github.com/twitterdev/cannonball-android (sample android app)
https://dev.twitter.com/twitter-kit/android/twitter-login
To get access to Fabric: fabric.io
This is what I did.
Before calling SettingsActivity.this.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(requestToken.getAuthenticationURL()))); I save the current activity in Sharedpreferences
When the splash activity comes up (the main that opens every time I open the app) I check this variable in Sharedpreferences and if it's true then I open up SettingsActivity and send the verifier with the intent.
In SettingsActivity now it's easy to log in with this verifier and get user data.
SplashActivity:
if (!isTwitterLoggedInAlready()) {
boolean fromSettingsActivity = mSharedPreferences2.getBoolean(TwitterConst.FROM_SETTINGS, false);
Uri uri = getIntent().getData();
if (uri != null && uri.toString().startsWith(TwitterConst.CALLBACK_URL)) {
final String verifier = uri.getQueryParameter(TwitterConst.IEXTRA_OAUTH_VERIFIER);
if (fromSettingsActivity) {
Intent intent = new Intent(Activity_Splash.this, SettingsActivity.class);
intent.putExtra("verifier", verifier);
startActivity(intent);
finish();
return;
} else {
new Connect_Twitter().execute(verifier);
}
}
}
SettingsActivity:
Intent iin = getIntent();
Bundle extras = iin.getExtras();
if(extras != null) {
String verifier = extras.getString("verifier");
new Connect_Twitter().execute(verifier);
}
And don't forget to reset the Sharedpreferences value when you click BACK:
#Override
public void onBackPressed() {
mSharedPreferences2 = getSharedPreferences(TwitterConst.FROM_SETTINGS, MODE_PRIVATE);
SharedPreferences.Editor e = mSharedPreferences2.edit();
e.remove(TwitterConst.FROM_SETTINGS);
e.commit();
super.onBackPressed();
}
One more thing
With the current intent the app opens up the SettingsActivity again, so you need to click BACK two times. This is the default behaviour of the Twitter login. To prevent this, use the intent like this:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(requestToken.getAuthenticationURL()));
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
I have a Form with edittexts and a button to call the camera with an intent (return a bitmap that is put into the imageview)...From the portrait mode i enter all edittext filed and then click the camera button which forwards me to the camera - in the camera i take a picture after what I get returned to Activity 1 (staying in portrait orientation - and all editext fields are restore in onRestoreInstanceState()) - and the last callback method of Activity 1 is onResume() (what is ok) - But the problem comes when I make an orientation change from this portrait to landscape mode - the callback methods are following
So the last callback orientation change is onPause(). I do not understand why? The problem is that onSaveInstanceState is called prior of onPause - so when I turn back to portrait mode everything will be empty (editexts, imageview..) - this strange behavior continues on every orientation change (the onPause() is called last).
I am sure this problem has to do something with the taking an image (startInentforResult....) because everything (editext fields) works fine on orientation change prior to taking an image...sometimes I can also take an image and it works fine, but in most cases not...
So my question is what is it that "drives" my Activity up to the onPause() method instead up to the onResume()?
Thanks, I would really appreciate if somebody knows the solution because I am struggling with this already a few days and could not find the solution.
The project has many classes but this is the activity code (Important to note is that the problem arises only when I take an image from camera app, after that the activity lifecycle goes crazy - also this activity is called from the main activity with 'startIntentforResult()'. I do not use 'android:configChanges="orientation|keyboardHidden"' to stop the recreatioin ):
public class NewCounterActivity extends Activity {
Button btnCreate;
Button btnCancel;
Button btnTakeImg;
ImageView counterImgView;
CheckBox existsDamage;
EditText inputNameFirst;
EditText inputNameLast;
EditText inputAdresse;
EditText inputCounterID;
EditText inputCounterValue;
EditText inputDescription;
TextView registerErrorMsg;
DatabaseHandler db;
//Data to be submitted
String nameFirst;
String nameLast;
String adresse;
String counterID;
String counterValue;
String countDescript;
String existsDmg;
Bitmap counterBitmap;
Bitmap recievedBitmap;
String longitude;
String latitude;
LocationTracker gps;
// JSON Response node names
private static String KEY_SUCCESS = "success";
private static String KEY_ERROR = "error";
private static String KEY_ERROR_MSG = "error_msg";
//The dimensions of the ImageView
int targetW;
int targetH;
// Some lifecycle callbacks so that the image can survive orientation change
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Log.e("onSaveInstanceState", "fadsfass");
outState.putParcelable("bitmap", counterBitmap);
outState.putString("fname", inputNameFirst.getText().toString());
outState.putString("lname", inputNameLast.getText().toString());
outState.putString("adrese", inputAdresse.getText().toString());
outState.putString("cID", inputCounterID.getText().toString());
outState.putString("cValue", inputCounterValue.getText().toString());
outState.putString("Descript", inputDescription.getText().toString());
outState.putString("ErrorMsg", registerErrorMsg.getText().toString());
outState.putBoolean("damageCheck", existsDamage.isChecked());
((MyApplicationClass) getApplication()).detach(this);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
Log.e("onRestoreInstanceState", "fadsfass");
counterBitmap = savedInstanceState.getParcelable("bitmap");
counterImgView.setImageBitmap(counterBitmap);
inputNameFirst.setText(savedInstanceState.getString("fname"));
inputNameLast.setText(savedInstanceState.getString("lname"));
inputAdresse.setText(savedInstanceState.getString("adrese"));
inputCounterID.setText(savedInstanceState.getString("cID"));
inputCounterValue.setText(savedInstanceState.getString("cValue"));
inputDescription.setText(savedInstanceState.getString("Descript"));
registerErrorMsg.setText(savedInstanceState.getString("ErrorMsg"));
existsDamage.setChecked(savedInstanceState.getBoolean("damageCheck"));
((MyApplicationClass) getApplication()).attach(this);
}
#Override
public void onContentChanged() {
// TODO Auto-generated method stub
super.onContentChanged();
Log.e("onContetnChanged", "fadsfass");
}
#Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.e("onDestroy", "fadsfass");
}
#Override
public void onDetachedFromWindow() {
// TODO Auto-generated method stub
super.onDetachedFromWindow();
Log.e("onDetachedFromWindow", "fadsfass");
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
Log.e("onPause", "fadsfass");
}
#Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
Log.e("onRestart", "fadsfass");
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Log.e("onResume", "fadsfass");
}
#Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
Log.e("onStart", "fadsfass");
}
#Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
Log.e("onStop", "fadsfass");
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.newcounteractivity_layout);
Log.e("onCreate", "mActivity equals NULL");
inputNameFirst = (EditText) findViewById(R.id.createFirstName);
inputNameLast = (EditText) findViewById(R.id.createLastName);
inputAdresse = (EditText) findViewById(R.id.createAdresse);
inputCounterID = (EditText) findViewById(R.id.createCounterID);
inputCounterValue = (EditText) findViewById(R.id.createCounterValue);
inputDescription = (EditText) findViewById(R.id.createDescription);
registerErrorMsg = (TextView) findViewById(R.id.create_error);
btnCreate = (Button) findViewById(R.id.btnCreate);
btnCancel = (Button) findViewById(R.id.btnCancel);
btnTakeImg = (Button) findViewById(R.id.btnImage);
counterImgView = (ImageView) findViewById(R.id.counterImgView);
existsDamage = (CheckBox) findViewById(R.id.createDamageExists);
//REGISTER BUTTON CLICK EVENTS
btnCreate.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//new DoBackgroundTask(NewCounterActivity.this).execute();
//CounterUser data to submit
nameFirst = inputNameFirst.getText().toString().trim();
nameLast = inputNameLast.getText().toString().trim();
adresse = inputAdresse.getText().toString().trim();
counterID = inputCounterID.getText().toString().trim();
counterValue = inputCounterValue.getText().toString().trim();
countDescript = inputDescription.getText().toString().trim();
existsDmg = Integer.toString((existsDamage.isChecked()) ? 1 : 0);
// create LocationTracker class object
gps = new LocationTracker(NewCounterActivity.this);
if(!gps.canGetLocation()){
gps.stopUsingGPS();
gps.showSettingsAlert();
//Ovo se mozda treba i izbaciti
gps.getLocation();
}
else{
processInput();
}
}
});
btnCancel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent returnIntent = new Intent();
setResult(RESULT_CANCELED, returnIntent);
finish();
}
});
btnTakeImg.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(isIntentAvailable(NewCounterActivity.this, MediaStore.ACTION_IMAGE_CAPTURE)){
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePictureIntent,2);
}
else {
Toast.makeText(NewCounterActivity.this, "No Camera Available", Toast.LENGTH_SHORT).show();
}
}
});
}
/************************************************************************************************
* Methods used in this class
* */
public void processInput(){
//Get current Longitude and Latitude
longitude = Double.toString(gps.getLongitude());
latitude = Double.toString(gps.getLatitude());
//Na kraju iskljuci location updatese - ne moze na emulatru jer ja emit coordinate preko DDMS... a kad emit on mora biti ukljucen da bi primio
//gps.stopUsingGPS();
Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + longitude + "\nLong: " + latitude, Toast.LENGTH_LONG).show();
if (!nameFirst.equals("") && !nameLast.equals("") && !adresse.equals("") && !counterID.equals("") && !counterValue.equals("")
&& counterBitmap != null ){
new DoBackgroundTask(NewCounterActivity.this).execute();
}
else{
// Not all fields are filled
registerErrorMsg.setText("Not all fields are filled");
}
}
//Method to check whether an app can handle your intent
public boolean isIntentAvailable(Context context, String action) {
final PackageManager packageManager = context.getPackageManager();
final Intent intent = new Intent(action);
List<ResolveInfo> list = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
return list.size() > 0;
}
/**************************************************************************************************
*
* When the calling activity, Activity #1, resumes after having called another activity, Activity #2, using startActivityForResult,
* the method onActivityResult in Activity #1 is called BEFORE onResume.
* This is important to know if you are instantiating your SQLite Database objects from within onResume in Activity #1. If so, you will also need to instantiate the object from within onActivityResult,
* when returning from Activity #2.
*
* startActivityForResult() is asynchronous. It can feel synchronous to the user since the UI will change and your calling activity will be paused
* (your onPause() method will be called).
*/
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
Log.e("onActivityResult", "fadsfass");
if (requestCode == 2) {
if(resultCode == RESULT_OK){
Bundle extras = data.getExtras();
recievedBitmap = (Bitmap) extras.get("data");
}
if (resultCode == RESULT_CANCELED) {
Toast.makeText(NewCounterActivity.this, "No Image Taken", Toast.LENGTH_SHORT).show();
}
}
}
/**
* Koristim onWindowFocusChanged jer kad se vratim na Activity 1 onda dodje do potpunog recreate Activitija i getWidth()/height() ne mogu dobiti
* ni u jednom od lifecicle methoda - naime ide start onCreate,...onActivityResult(), onResume() - u onactivityResult izvadim bitmap i pohranim ga u receivedBitmap
* te kad getWidth() postane dostupan system invoke ovu dole methodu. :D
*/
#Override
public void onWindowFocusChanged(boolean hasFocus){
if(recievedBitmap != null){
targetW=counterImgView.getWidth();
targetH=counterImgView.getHeight();
Log.e("onWindowFocusChanged", "fadsfass" + " " + targetW + " " + targetH);
// http://stackoverflow.com/questions/4837715/how-to-resize-a-bitmap-in-android
// http://sunil-android.blogspot.com/2013/03/resize-bitmap-bitmapcreatescaledbitmap.html
// Scale or resize Bitmap to ImageView dimensions
counterBitmap = Bitmap.createScaledBitmap(recievedBitmap, targetW, targetH, false);
/**
* Canvas: trying to use a recycled bitmap android.graphics - This exception occurs when you try to recycle a bitmap which is already recycled.
* http://androdevvision.blogspot.com/2011/10/solution-for-out-of-memory-error-and.html
*/
if(recievedBitmap != null && !recievedBitmap.isRecycled()){
recievedBitmap.recycle();
recievedBitmap = null;
}
counterImgView.setImageBitmap(counterBitmap);
}
}
/************************************************************************************************
* Background AsyncTask to create new counterUser - https://github.com/callorico/CustomAsyncTask - najbolje radi
* new DoBackgroundTask(NewCounterActivity.this).execute();
* */
private static class DoBackgroundTask extends CustomAsyncTask<Void, Integer, JSONObject> {
private static final String TAG = "DoBackgroundTask";
private ProgressDialog mProgress;
private int mCurrProgress;
private NewCounterActivity myActivity = null;
public DoBackgroundTask(NewCounterActivity activity) {
super(activity);
}
#Override
protected void onPreExecute() {
super.onPreExecute();
showProgressDialog();
}
#Override
protected void onActivityDetached() {
if (mProgress != null) {
mProgress.dismiss();
mProgress = null;
}
}
#Override
protected void onActivityAttached() {
showProgressDialog();
}
private void showProgressDialog() {
mProgress = new ProgressDialog(mActivity);
mProgress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
mProgress.setIndeterminate(true);
mProgress.setMessage(" Saljem na server... ");
mProgress.setCancelable(true);
mProgress.setOnCancelListener(new OnCancelListener() {
#Override
public void onCancel(DialogInterface dialog) {
cancel(true);
}
});
mProgress.show();
mProgress.setProgress(mCurrProgress);
}
#Override
protected JSONObject doInBackground(Void... params) {
//so you need to either pass an instance of the outer class to the inner class method (or its constructor) as a parameter,
//or create it inside the method.
JSONObject json = null;
if(mActivity != null){
myActivity = (NewCounterActivity) mActivity;
//Prepare counterBitmap as String
ByteArrayOutputStream stream = new ByteArrayOutputStream();
//Write a compressed version of the bitmap to the specified output stream.
myActivity.counterBitmap.compress(Bitmap.CompressFormat.JPEG, 90, stream);
byte [] b_array = stream.toByteArray();
String bitmapString = Base64.encodeBytes(b_array);
//Get workerId from logged worker
Functions workerFunction = new Functions();
DatabaseHandler db = new DatabaseHandler(mActivity);
String workerID = db.retrieveWorker().get("workerId");
if(myActivity != null){
//Get JsonObject from Functions.java
json = workerFunction.newCounterUser(myActivity.counterID, myActivity.counterValue, myActivity.adresse, myActivity.nameFirst, myActivity.nameLast, bitmapString, myActivity.existsDmg, myActivity.countDescript, workerID, myActivity.longitude, myActivity.latitude);
}
}
return json;
}
#Override
protected void onPostExecute(JSONObject jsonObject) {
super.onPostExecute(jsonObject);
if (mActivity != null) {
mProgress.dismiss();
try {
if (jsonObject.getString(KEY_SUCCESS) != null) {
myActivity.registerErrorMsg.setText("");
String res = jsonObject.getString(KEY_SUCCESS);
if(Integer.parseInt(res) == 1){
// counterUser successfully registered
Toast.makeText(mActivity, "New counterUser is created", Toast.LENGTH_LONG).show();
// Return back to MainActivity
Intent returnIntent = new Intent();
returnIntent.putExtra("result",jsonObject.toString());
mActivity.setResult(RESULT_OK,returnIntent);
// Close all views before launching MainActivity
returnIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
mActivity.finish();
}else{
// Error in registration
myActivity.registerErrorMsg.setText("Error occured in registration");
}
}
} catch (JSONException e) {
Log.e("Error","NO Json at all");
e.printStackTrace();
}
} else {
Log.d(TAG, "AsyncTask finished while no Activity was attached.");
}
}
}
Same issues on call to recreate(), when activity has been updated it get onPause after onResume. Tested on emulator, bug exists on Marshallow and below.
This is my fix
private static boolean isRecreate = false;
private void reCreateActivity() {
isRecreate = true;
recreate();
}
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if(isRecreate) {
isRecreate = false;
runOnUiThread(new Runnable() {
#Override
public void run() {
onResume();
}
});
}
}
I don't know if that's correct, but it works.
EDIT
Best solution to avoid this issues, call recreate in postDelayed with 0 delayMillis
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
recreate();
}
}, 0);
It is only normal, that strange code produces strange behavior ...
replace this line:
((MyApplicationClass) getApplication()).detach(this);
with this line:
super.onSaveInstanceState(outState);
I'm stuck with a memory leak that I cannot fix. I identified where it occurs, using the MemoryAnalizer but I vainly struggle to get rid of it. Here is the code:
public class MyActivity extends Activity implements SurfaceHolder.Callback {
...
Camera.PictureCallback mPictureCallbackJpeg = new Camera.PictureCallback() {
public void onPictureTaken(byte[] data, Camera c) {
try {
// log the action
Log.e(getClass().getSimpleName(), "PICTURE CALLBACK JPEG: data.length = " + data);
// Show the ProgressDialog on this thread
pd = ProgressDialog.show(MyActivity.this, "", "Préparation", true, false);
// Start a new thread that will manage the capture
new ManageCaptureTask().execute(data, c);
}
catch(Exception e){
AlertDialog.Builder dialog = new AlertDialog.Builder(MyActivity.this);
...
dialog.create().show();
}
}
class ManageCaptureTask extends AsyncTask<Object, Void, Boolean> {
protected Boolean doInBackground(Object... args) {
Boolean isSuccess = false;
// initialize the bitmap before the capture
((myApp) getApplication()).setBitmapX(null);
try{
// Check if it is a real device or an emulator
TelephonyManager telmgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String deviceID = telmgr.getDeviceId();
boolean isEmulator = "000000000000000".equalsIgnoreCase(deviceID);
// get the bitmap
if (isEmulator) {
((myApp) getApplication()).setBitmapX(BitmapFactory.decodeFile(imageFileName));
} else {
((myApp) getApplication()).setBitmapX(BitmapFactory.decodeByteArray((byte[]) args[0], 0, ((byte[])args[0]).length));
}
((myApp) getApplication()).setImageForDB(ImageTools.resizeBmp(((myApp) getApplication()).getBmp()));
// convert the bitmap into a grayscale image and display it in the preview
((myApp) getApplication()).setImage(makeGrayScale());
isSuccess = true;
}
catch (Exception connEx){
errorMessageFromBkgndThread = getString(R.string.errcapture);
}
return isSuccess;
}
protected void onPostExecute(Boolean result) {
// Pass the result data back to the main activity
if (MyActivity.this.pd != null) {
MyActivity.this.pd.dismiss();
}
if (result){
((ImageView) findViewById(R.id.apercu)).setImageBitmap(((myApp) getApplication()).getBmp());
((myApp) getApplication()).setBitmapX(null);
}
else{
// there was an error
ErrAlert();
}
}
}
};
private void ErrAlert(){
// notify the user about the error
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
...
dialog.create().show();
}
}
The activity is terminated on a button click, like this:
Button use = (Button) findViewById(R.id.use);
use.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MyActivity.this, NextActivity.class);
intent.putExtra("dbID", "-1");
intent.putExtra("category", category);
((myApp) getApplication()).setBitmapX(null);
MyActivity.this.startActivity(intent);
MyActivity.this.finish();
}
});
MemoryAnalyzer indicated the memory leak at:
((myApp) getApplication()).setBitmapX(BitmapFactory.decodeByteArray((byte[]) args[0], 0, ((byte[])args[0]).length));
I am grateful for any suggestion, thank you in advance.
Is your thread garbage collected after onPostExecute is called or is it still in the memory?
A Async Task will not be canceled or destroyed at the moment the activity is dismissed. If your thread is more or less lightweight and finishes after a small time, just keep it running and add a MyActivity.this.isFinishing() clause in the onPostExecute() method.
Your Task stores a implicit reference to your Activity MyActivity.this because it is a private class inside the activity. This means that your Activity will not be garbage collected until the task exits.
You can try below code snippet
protected void onPostExecute(Boolean result) {
if(YourActivity.this.isFinished()){
//to smomething here
}
}