reading the https://developer.android.com/topic/performance/vitals/launch-time about the right way to implement a launch screen they say to create a Launcher style
<activity ...
android:theme="#style/AppTheme.Launcher" />
and they say that the easiest way to transition back to your normal theme is to call setTheme(R.style.AppTheme) before calling super.onCreate() and setContentView():
KOTLIN
JAVA
public class MyMainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// Make sure this is before calling super.onCreate
setTheme(R.style.Theme_MyApp);
super.onCreate(savedInstanceState);
// ...
}
}
problem is with delphi, when/where can I call setTheme(R.style.Theme_MyApp); ? from inside the form create it's not work so seam already too late, and even just after Application.Initialize it's seam to not work either :(
Related
I'm creating my first library to use in some future projects, on it I'm creating some Activities that are the same on every project.
Right now I'm working with a test project and on my library I have this LoginActivity. It has it's own layout and works fine.
How can I make my test app LoginActivity be the one from the library? At the moment I'm extending my LoginActivity from the library into my activity on the project. Is this the right way of doing it considering that all the code logic happens on the Library activity?
Library LoginActivity
public class LoginActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
// Some useless code...
}
}
Project Login Activity
public class MainActivity extends LoginActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Nothing happens in this class... really...
}
}
Make sure that Activity that is inside of the library project is declared inside of the library's manifest file. Then start the activity how you normally would start any other activity. If you want the activity to be the first activity when the app launches (launcher activity), you need to declare it explicitly inside of your manifest and add an intent-filter tag element as a child, that indicates it should be the launcher activity. View this post.. Another way to roughly achieve the same effect would be to create a blank activity that is declared in the app manifest file as the launcher activity and inside of the onCreate method launch the library activity. Make sure you clear any and all activity transitions so that the switch to the library activity is seamless. The benefit to this approach is that you can perform a check before launching the library activity.
Ex. checking if the login activity needs to be displayed or if the screen can be by-passed and the app can be entered immediately.
Add login activity into AndroidManifest.xml in library or application.
<application>
<activity
android:name="com.mypackage.LoginActivity" />
</application>
Basically, I have an app with two Activities.
#1 - MainActivity
This has a solid black background and a button.
When the button is pressed TransparentActivity should be presented.
#2 - TransparentActivity
I want this to be transparent (so the phones normal UI can be seen through).
I've tried using the following code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(android.R.style.Theme_Translucent);
setContentView(R.layout.activity_trick);
}
But it causes the app to crash with an NullPointerException.
Try1:
Make super.onCreate(savedInstanceState); call after setTheme(android.R.style.Theme_Translucent);. So do as:
setTheme(android.R.style.Theme_Translucent);
super.onCreate(savedInstanceState);
Try 2:
If that doesn't work, I find the following way easiest to make my activity transparent:
<activity android:name=".your.activity.declaration.here"
android:theme="#android:style/Theme.Translucent.NoTitleBar" />
Basically add android:theme="#android:style/Theme.Translucent.NoTitleBar" to your activity declaration in manifest. I can see that you are trying to do a similar thing programatically but by specifying it in manifest never crashed for me. If it does, then there might be other reasons.
Hope it helps.
AppCompatActivity hasn't the Theme_Translucent(maybe the Theme_Translucent is null),you should create your own style.
I made 2 activities.
e.g] MainActivy and MediaActivity.
If user click home button in MediaActivity, App will hide.
I want launch MediaActivty again when screen on.
Open the gmail app, tap on an email to show it's contents. You have now gone from one activity (email list) to another (email details). Press the home button. Now open the gmail app again. Voila! You have returned to the email you selected.
What you ask for is the default behavior of an Android app. Unless you do something like call finish() in your onPause() method in MediaActivity you should return to MediaActivity when you open the app again.
If your app doesn't behave like this I suggest that you post some code.
Try to write your activities the following way :
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Put your own code here.
}
}
public class MediaActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Put your own code here.
}
}
What you're asking is Up Navigation / Ancestral Navigation(if I understood it correct). Refer to this guide: http://developer.android.com/training/implementing-navigation/ancestral.html
Othewise, if you want to have multiple instances of an activity, it's android:launchMode property should be set to standard in manifest. Refer:
http://developer.android.com/guide/topics/manifest/activity-element.html
I realise you can set the LAUNCHER activity of your app in the manifest file, but is there anyway you can statically do this in code before the activity is loaded by the Dalvik VM? Something like:
public class MyActivity extends Activity{
RunTime.LAUNCHER = MyActivity.class
...
}
I realise this might not be possible, but if it is I would appreciate a safe and reliable code example to achieve this?
Many thanks
What is possible, however, is to have a first empty activity that starts whatever activity you need next, without displaying itself.
public void onCreate(Bundle stuff) {
super.onCreate(stuff);
startActivity(new Intent(...whatever...);
finish();
}
I have 2 Activities : First activity user clicks on a button which launches the 2nd activity. The 2nd Activity does all the work.
I launch the 2nd Activity as follows which is inside a onClickListener Inner Class and I have tried explicitly calling it with (FirstActivity.this,Simple.Class) but same thing happens.
Intent test = new Intent(arg0.getContext(),Simple.class);
startActivity(test);
On the emulator, I see the screen move over like its calling the 2nd activity but all I get is a black screen but nothing is loaded from my layout. I looked at logcat and I do see some binder thread failed messages. This is the onCreate function from my 2nd activity but I do not get any results from either the screen or logcat showing me that the Log functions were called:
public void onCreate(Bundle savedState)
{
Log.d("SimpleActivity","OnCreate Started");
super.onCreate(savedState);
setContentView(R.layout.simple);
Log.d("SimpleActivity","OnCreate Ended");
}
Note : I have called the base constructor in OnCreate() with super.onCreate(savedState) in my code above.
What happened to me was I was overriding the wrong onCreate method. I was overriding public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) when I really needed to override protected void onCreate(#Nullable Bundle savedInstanceState). Maybe this might help someone!
It's possible that onCreate doesn't get called, if the activity has never been destroyed,
if for some reason an activity hangs around, next time its instantiated it is not recreated but resumed instead...
At least that's what im Dealing with right now in my code... Life cycle of Activities seem a good logical explanation.. However 99% of time I do rely on onCreate being called when startingActivity and it doesn't fail me....
Edit: And of course its because I wasn't calling finish() when exiting the activity. Doh.
This is not related to this certain issue, but also this can happen when activity is not declared in manifest file)
Be careful that if your method belongs to AppCompatActivity or Activity .
It is up to what you implemented to your Class
If you want to add lifecycle or any override methods, I recommend you to press
CTRL+O or do Code > Override methodsand there you can see where the method belongs
remove android:launchMode="singleTask" from manifest
you should #Override onCreate and add super.onCreate() in it
#Override
public void onCreate(Bundle savedState)
{
super.onCreate(savedState);
Log.d("SimpleActivity","OnCreate Started");
setContentView(R.layout.simple);
Log.d("SimpleActivity","OnCreate Ended");
}
my case
(1) mainActivity -> (2) open Adaptor - startActivity -> (3) mainActivity onCreate() doesn't get to triggered.
I resolved this by adding finish();. in mainActivity.
follow the below steps to check your application.
1.did you override the right method? if not overriding the below method, this method will be triggered, when you startActivity.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
2.Make sure that you registered the activity in manifest.xml
2.1 is your activity has android:launchMode="singleInstance" ?
(if your application doesn't need to be singleinstance, consider to remove.
but my case I need singleinstance. hence i moved to the next step)
use finish()
public void openSearch(View view) {
Intent intent = new Intent(this, BookInfoActivity.class);
intent.putExtra(...);
startActivity(intent);
finish(); // add like this.}
why do we need to use "finish()"?
Screen A -> click button on A -> Screen B -> click button on B -> screen A with some new data that you get from Screen B
if you don't call finish() method(in A button) , that means the A is still in your background
even you are seeing the screen B.
hence, when you trigger startActivity on screen B, it just simply shows the running A screen.
however if you use finish() method (in A button), when you go to B Screen,
it destroys the A screen, so when you go back to A screen by clicking B method( 'StartActivity') it creates A screen and trigger onCreate() Method .
You need to call the super.onCreate(savedState) method. Take a look at Activity doc.
public void onCreate(Bundle savedState)
{
super.onCreate(savedState);
}