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>
Related
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 :(
I would like to have an appwide rule to not allow the landscape orientation. I realize I could get this by putting:
android:screenOrientation="portrait"
in every one of my activities but that doesn't seem clean. I can also put
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
in every one of my activity's onCreate() methods.
But I desire a solution that doesn't require this code duplication. Any suggestions?
Solution:
You can do this for your entire application without having to make all your activities extend a common base class.
The trick is first to make sure you include an Application subclass in your project. In its onCreate(), called when your app first starts up, you register an ActivityLifecycleCallbacks object (API level 14+) to receive notifications of activity lifecycle events.
This gives you the opportunity to execute your own code whenever any activity in your app is started (or stopped, or resumed, or whatever). At this point you can call setRequestedOrientation() on the newly created activity.
class MyApp extends Application {
#Override
public void onCreate() {
super.onCreate();
// register to be informed of activities starting up
registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
#Override
public void onActivityCreated(Activity activity,
Bundle savedInstanceState) {
// new activity created; force its orientation to portrait
activity.setRequestedOrientation(
ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
....
});
}
}
Any doubts, Please leave a comment.
Note: Add in manifest's <application> tag: android:name=".MyApp"
Hope it helps.
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();
}
In my application there are 14 activities. Out of that 9 activity contains custom title bar and tab pane. so here I need to write this common code at one place instead of redundant code in each activity that contain custom title bar and tab pane code (i.e layout and it's activity specific code)
What are the possible ways to do this?
The common way is:
Create a super class called, for instance, CommonActivity which extends Activity
Put the boilerplate code inside that class
Then make your activities extend CommonActivity instead of Activity:
Here a simple example:
public class CommonActivity extends Activity{
public void onCreate(Bundle b){
super.onCreate(b);
// code that is repeated
}
protected void moreRepeatitiveCode(){
}
}
And your current activities:
public class AnActivity extends CommonActivity{
public void onCreate(Bundle b){
super.onCreate(b);
// specific code
}
}
Hmm.. Common code doesn't always need to be in Activity class but just regular class. Than we could call those methods according to our needs referring to the common code class.
Am I right with this example?
Of course in case we need it like Activity, above proposal would work perfectly if we take care of Activity lifecycle and we don't forget to add it to manifest file.
In general Activities should just create UI, handle events occurrences and delegate business logic and/or other actions to the other components in our App.
Cheers