Issues with Translucent Theme - android

I have an app that has two activities.
The first one is presented with a single button that opens the second one.
Here is the Manifiest definition for the first one:
<activity
android:name="com.example.buttonexample.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Second activity:
<activity
android:name="com.example.buttonexample.MainActivity2"
android:label="#string/title_activity_main_activity2" android:theme="#android:style/Theme.Translucent">
</activity>
Here is how I launch the second activity (via OnClickListener for a button on the first activity):
public void startSecondActivityClick(View v) {
Intent startActivity2 = new Intent(this, MainActivity2.class);
startActivity(startActivity2);
}
This works fine, however when I background the app by hitting home and the foreground the app. I'm noticing that the first activity is continually creating/destroying itself. I verified this by putting some code in the onDestory method to increment a static int:
private static int count = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
count++;
}
protected void onDestroy() {
super.onDestroy();
Log.i("MainActivity", String.format("Destroyed, %d", count));
}
I've also noticed that removing the translucent theme seems to fix this. My question is is there a way to translucent or something similar but also have it not restart? Also, I'm curious why this happens at all. I'm testing this on 4.0.1 ICS on a galaxy SIII.

Ok after some digging I was able to figure out why this is happening. Someone had turned on one of the developer options, "do not keep activities.". After turning this off this stopped happening. I suspect this wouldn't happen in production too often as most people probably don't have that setting on. You can find this under settings -> "developer options" on most phones.

Related

Changing Main Activity in Manifest result to shortcuts doesn't work anymore

I try to add welcome tutorial for users that install application for the first time. That activity need to be declare as Main in Manifest (or I miss something?). But if I choose any other activity else than main one (which is actual app), app shortcuts (Android 7.1) doesn't work anymore. It's interesting however that shortcuts are still available at custom launchers (Apex, Nova). Any idea?
(almost all) Google apps has welcome tutorial as well as launcher shortcuts. I can't get it how they did it?
A welcome tutorial does not have to be an activity. It could be some other sort of presentation (e.g., a fragment).
A welcome tutorial, even if it is another activity, does not have to be the launcher activity. The launcher activity could detect that it is the first run and started the tutorial activity.
Thank you for the answers CommonWare! Your statements helps me to find answer. So, I want to start an app which shows Splash screen, then Welcome tutorial. Also, app need working shortcuts on main screen as well as only one launcher icon. So, first, I declare Splash screen as main in Manifest.xml:
<activity
android:name=".SplashActivity"
android:noHistory="true"
android:theme="#style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.app.shortcuts"
android:resource="#xml/shortcuts" />
</activity>
Then, Welcome (tutorial) activity:
<activity
android:name=".IntroActivity.WelcomeActivity"/>
After that, in SplashActivity.class checking first launch:
public static final String FIRST_APP_LAUNCH = "com.ips.test";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (isFirstAppLaunch()) {
setFirstAppLaunch(false);
startActivity(new Intent(this, WelcomeActivity.class));
} else {
startActivity(new Intent(this, MainActivity.class));
}
finish();
}
private boolean isFirstAppLaunch() {
SharedPreferences preferences = this.getPreferences(Context.MODE_PRIVATE);
return preferences.getBoolean(FIRST_APP_LAUNCH, true);
}
private void setFirstAppLaunch(boolean value) {
SharedPreferences preferences = this.getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean(FIRST_APP_LAUNCH, value);
editor.apply();
}
}
Final result is as I wanted: app launch with Splash screen, then it runs Welcome tutorial. Next start will trigger Splash screen which will continue to main activity (app itself). When user click on shortcut in main screen it will get shortcuts, and in Launcher it will have just one application shortcut.

Draw over other app service reopens itself after closing

I would like to apologize if this is somehow a repeated question that has been answered. I have tried the solutions I was able to find here and still not able to do the thing I need.
I am still trying to learn programming with android system and currently trying to do something like the chat bubble that draws over other app. From learning on this site, I was able to do it by opening an intent, but I have not been able to find a way to close the app completely--even after swiping it to close after pressing the home key for a short period of time, the app will automatically reopens itself. Let me try to explain it better with what I am doing.
In manifest:
I have
and these:
<activity
android:name=".Play"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name="com.example.play.MainClass"
android:exported="true" />
In Play.java
public class Play extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent svc = new Intent(this, MainClass.class);
startService(svc);
}
#Override
public void onDestroy() {
super.onDestroy();
}
}
In MainClass.java
I am using this
public class MainClass extends Service {
.......
}
as well as using
private WindowManager wm;
and do the codes using the wm to display the chat bubble i need.
I have been trying to search how to close android app programmatically and phrases like that and I found that many answers include doing:
System.exit(0);
or
finish();
or
Process.killProcess(int pid)
But then again, I am not able to close my app completely--it runs again by itself after around 10-20seconds. I am thinking it is because I am using 'Service' so I am not able to close the app completely.
I am looking forward to any of your kind reply and patience.
I apologize in advance for if this is still answered somewhere that I was not able to find it.
Thank you very much.

How to avoid my activity from restarting when I go back to main menu?

I'm new programming in android. Recently I've been doing a project with Android studio.
In my application, I'm creating an activity with two options for the user with two toggle buttons.
The user can choose if the buttons make a sound or vibrate.
The toggle button works perfectly, but when the user returns to the Main Activity and comes again to the options, the toggle button doesn´t save the settings that the user has chosen.
Example:
(Go to Options)/ in Options the user chooses Sound/off & Vibrate/off
back to (Main Activity)
(GO to Options Again) The settings are restarted Sound/on & Vibrate/on
I hope someone can help me with this problem!
Android has a facility for persisting Activity (or Fragment) state.
If your Activity is put into background it might be destroyed at some point and you don't have control over it. This is why Android uses Bundles. Store your data inside the bundle then read it back in
onCreate(Bundle savedInstanceState)
so before your activity dies for any reason, store important values:
protected void onSaveInstanceState(Bundle bundle) {
bundle.putInt(SOME_IMPORTANT_INT,mMyInt);
bundle.putString(SOME_IMPORTANT_STRING,mMyString);
}
when activity is created we have to read what is in the bundle (if there is anything at all):
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(savedInstanceState != null) {
mMyInt = savedInstanceState.getInt(SOME_IMPORTANT_INT);
mMyString = savedInstanceState.getString(SOME_IMPORTANT_STRING);
}
}
In short, what ever happens to your activity mMyInt and mMyString values are restored.
We might have a little control over when activity are restarted. This is done by android:configChanges attribute in the AndroidManifest file:
<activity
android:name=".ui.login.LoginActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
http://developer.android.com/guide/topics/resources/runtime-changes.html

Android home screen launcher

First of all let me tell you guys that I am completely newbie to Android app development.
I want to know how to launch home screen and then application home screen.
i.e whenever you click on some app, first it will show some logo screen and then automatically jump to home screen of the app.
I have my home screen of the app running, I just want to add some kind of logo screen when user click on the app.
I did little bit research on this and I found out
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
If I add this to logo activity then how to call app_home_screen activity automatically
Hope I am clear enough to explain the question.
You need to:
Add a new Activity SplashActivity which will be the start Activity of your app.
When your app starts, launch your SplashActivity and wait for some time.
When you finish waiting, start your MainActivity and finish the SplashActivity.
Here is a good example:
SplashScreenActivity:
public class SplashScreenActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_layout); //You need to define it in your layouts
final int welcomeScreenDisplay = 3000; //splash lasts for 3 sec. You can change it
/** create a thread to show splash up to splash time */
Thread welcomeThread = new Thread() {
int wait = 0;
#Override
public void run() {
try {
super.run();
while (wait < welcomeScreenDisplay) {
sleep(100);
wait += 100;
}
} catch (Exception e) {
System.out.println("EXc=" + e);
} finally {
startActivity(new Intent(SplashScreenActivity.this, MainScreenActivity.class));
finish();
}}
};
welcomeThread.start();
}
}
And don't forget to add the activities to your Manifest file:
<activity
android:name=".SplashScreenActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainScreenActivity"
android:label="#string/app_name" >
</activity>
Although the other answers certainly work, here is an alternative to avoid having a separate activity just for your logo:
Place your ImageView as you would like (i.e. covering your full screen) before the other elements in your main activity layout (they will have an initial layout outside of the screen).
After calling setContentView in onCreate, run SystemClock.sleep(LOGO_TIME), where LOGO_TIME is an integer representing the number of milliseconds you want your logo to appear.
Finally, call setVisibility(View.GONE).
Your ImageView will be gone, and the other layout elements will fall into its place.
One solution you could attempt is to load the logo as your main activity, and have the application pause for a few seconds before calling the 2nd activity, your 'true' home page.
To start that 2nd activity you'll use 'Intent' such as
Intent myIntent = new Intent(myFirstActivity.this, realHomePage.class);
myFirstActivity.this.startActivity(myIntent);
Just make sure to extend the Activity class in your home page class
Have the main activity that gets called when your app icon is clicked on be an activity which has a view with only your logo on it. Something simple, just a RelativeLayout and an ImageView. After a time delay, have this activity start your actual main activity with an intent.

Android: I want to create a new file in sdcard when the apk is installing

I want to create a new file in sdcard when the apk is installing...
How or where should I code?
Thanks in advance!
You definitely can not do that. That would introduce all kinds of security concerns. As Ken Y -N said in his answer, the best thing to do would be to detect the first time that your app is opened, and do something there.
Here's an activity class to do this:
public class StartupChoiceActivity extends Activity {
private static final String TAG = StartupChoiceActivity.class.getSimpleName();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences mPrefs = getSharedPreferences("prefs", Context.MODE_PRIVATE);
if(!mPrefs.getBoolean("has_started_before", false)) {
// Do what ever you want to do the first time the app is run
} else {
//Remember our choice for next time
mPrefs.edit().putBoolean("has_started_before", true).commit();
Log.i(TAG, "We've already started the app at least once");
}
//Do what ever we want to do on a normal startup. This is pretty much always mean starting a new activity
startActivity(new Intent(this, MyNormalFirstActivity.class);
finish();
}
}
The only other thing you need to do is make sure this activity is set as your 'Startup' activity in the AndroidManifest.xml. You can do this by putting this code inside your application tag:
<activity android:name=".StartupChoiceActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
You cannot do that; such a feature would be a good way of getting trojans to propagate, for instance.
Just check for the first run of your program and do the initialisation there.

Categories

Resources