I want to open an activity without declaring it in an manifest file.
I don't know if it is possible or not.
What I actually want is to dynamically open an activity from my program using intents.
Can anyone help me if it is possible.
Not possible. Although I am unsure what you mean "dynamically open an activity".
See: http://developer.android.com/reference/android/app/Activity.html
Under Class Overview it states "To be of use with Context.startActivity(), all activity classes must have a corresponding declaration in their package's AndroidManifest.xml"
You can have an Activity in your package and not define it in the manifest, however you would not be able to start it successfully.
Your 'Dynamic' activity start is actually the normal way of starting an activity (as you have said in a comment to the answer of Matt M). Though you HAVE to add the Activities in manifest as Matt M said. In list view, clicking an activity will call a function that will start respective activity with startActivity() function.
I tried this very long, but since the Instrumentation class uses the IActivityTaskManager, which is a class of the internal API located at com.android.server.wm, that is not in the Activity classloader, I solved this by another method:
Using a subclass
I just made an Gist, with sample code.
GIST
Related
startActivity(new Intent("com.google.android.accessibility.talkback.tutorial.AccessibilityTutorialActivity"));
getting crash
Exception: No Activity found to handle Intent { act=com.google.android.accessibility.talkback.tutorial.AccessibilityTutorialActivity }
android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:2064)
android.app.Instrumentation.execStartActivity(Instrumentation.java:1718)
android.app.Activity.startActivityForResult(Activity.java:5211)
androidx.activity.ComponentActivity.startActivityForResult(ComponentActivity.java:574)
android.app.Activity.startActivityForResult(Activity.java:5169)
androidx.activity.ComponentActivity.startActivityForResult(ComponentActivity.java:560)
android.app.Activity.startActivity(Activity.java:5540)
androidx.core.content.ContextCompat.startActivity(ContextCompat.java:251)
androidx.fragment.app.FragmentHostCallback.onStartActivityFromFragment(FragmentHostCallback.java:166)
androidx.fragment.app.Fragment.startActivity(Fragment.java:1377)
androidx.fragment.app.Fragment.startActivity(Fragment.java:1365)
i need to launch the talkback tutorial activity
The exception you're seeing suggests that the system cannot find the activity you're trying to launch. There are a few reasons why this might be happening.
First, make sure that the package name and activity name you're using is correct. It's possible that the name of the activity has changed in a recent update or you've made a typo.
Next, check that the activity is declared in the AndroidManifest.xml file for the application. Every activity that can be launched from outside the application needs to be declared in the manifest.
Finally, it's possible that the activity requires specific permission that your app does not have. Check the documentation for the activity to see if any permissions are required.
We know that xml layout code gets parsed to java view object in setContentView call. But I was just curious to know when are the manifest properties (like windowSoftInputMode, screenOrientation etc) are applied to the activity. I mean what lifecycle method?
Any practical explanation is more than appreciated, TIA!
Well, I think manifest will get attached to the app in its installation period. like when we start installing an app , it will read its manifest file to check ;
where to install
What permissions to show
which activity is a launcher for the app
for more information:
https://developer.android.com/guide/topics/manifest/manifest-intro
I came across a problem in a job interview that whether I can dynamically add a new activity to an Android application without releasing a new version of the app. And he told me that there exists certain mechanism that we could dynamically change the Activity to a new one, without registering in the AndroidManifest.xml file. I searched some documents, but did not found possible way to do this.
Can I start an Activity without registering in the AndroidManifest.xml file? And is it possible to dynamically modify the existing Activity?
Can I start an Activity without registering in the AndroidManifest.xml file?
No. I don't think you can start an activity that is not registered in the AndroidManifest.xml file. The manifest file keeps track of the activities that the app can use upon compiling/building the application. Any attempt to open an unregistered activity will result in an application crash.
And is it possible to dynamically modify the existing Activity?
As Bette Devine said, you can change the layout of the existing activity by calling setContentView(R.layout.new_layout); based on some user action (like a button press). However, calling setContentView more than once in your activity is a bad practice that people generally avoid doing. It is not recommended since you'll have to write code that would manage user interaction for the second layout. Imagine writing two activity codes in one java file. That would result in unnecessary clutter of code when you can just write them separately.
Yes it is possible to dynamically modify an existing one.
Here modification does not mean that you are changing the name of an activity but means that you are changing the content.
Just call the setContentView method of activity to give a new layout to the activiy and you whole activity now will be hosting a different content.
setContentView(R.layout.new_layout);
I'm new to this and I'm sorry if this is a really dumb question. I'm just trying to clarify things. My book says I can retrieve application context for process by using the getApplicationContext() method. I just really don't know where to type this or what to do with any of it. I can go to the hierarchy but what do I do with all the script there. Also where would I write Activity Callbacks, in the main.xml? An exercise wants me to add a logging tag to my project but I'm not sure how to do this. The exact text says:
"Within the onCreate() callback method, add an informational logging message, using the Log.i() method."
and another exercise says to:
"Implement some of the Activity callback methods in addition to onCreate(), such as onStart(). Add a log message to each callback method and then run the application normally".
As these seem like basic questions, can someone please help me.
I am using the Android SDK, and Eclipse. I have made the Hello World application, but I have no idea what to do with Context or Retrieving resources. Please help!
The first rule I would give you: if you don't know why you need it, you probably don't need it. Use your activity object as the Context when you need a context.
The callbacks you talk about are on the Activity class. The Application Fundamentals describes what an Activity is: http://developer.android.com/guide/topics/fundamentals.html#Components
The only time you want to use getApplicationContext() is when you need a Context that exists outside of the lifecycle of an Activity class (or other component). You'll want to find documentation on specific cases where this is desired, there is a lot floating around. For example this one is part of the Android documentation: http://android-developers.blogspot.de/2009/01/avoiding-memory-leaks.html
For the tasks you're working with here, you'll be using the Java code that defines the behavior of the application, not the XML files that define resources and layouts or the AndroidManifest.xml file that declares basic application properties.
If you're working with Hour 3 of the Sam's Teach Yourself... book, then you need to open the src\com.androidbook.droid1\DroidActivity.java file. In general, you would need src\<package-name>\<class-name>.java. When you open that file, you'll see a class (in this case, DroidActivity) that extends Activity and already has the onCreate() callback method. Anything that you want to happen during onCreate() goes inside that method. Other callback methods can be added inside the activity class. To see an example that has all the lifecycle callbacks (but doesn't do anything in them), look here.
A logging tag is just a string. You can declare it, for example, as a private static final String inside the activity class.
If there's confusion about where methods belong, where and how to define variables or constants, how to call methods, how to use classes, and so forth, then it might be best to go through an introductory Java text before starting with Android. There are plenty of free resources available for that.
I need to start android application not with Activity^ but with some controller class that will start some activity
Is it possible?
I'm not sure if I understand your question correctly, but an Android application is built up by four "components" as mentioned in the "Android Application Fundamentals", http://developer.android.com/guide/topics/fundamentals.html (no, you don't need all four of them make your application work).
The most common way of starting an application (and actually the only one I've been in touch with) is to define an Activity in your applications AndroidManifest.xml file as described on the link above. NOTE! that an Activity doesn't have to define a UI; you are not obligated to call the "setContentView()" function. Hence your "controller class" can extend Activity and be the very Activity you define as the start-up Activity in your manifest xml. You can then call "startActivity()" function with parameters to start any other Activity, whenever you see fit, from your controller class (this is also described in the link above).
Hope this helps.
Either create a GUI-less activity without calling setContentView() or use a BroadcastReceiver that accepts launcher intents (action=MAIN, cateogry=LAUNCHER). In Activity.onCreate or receivers callback method you can place logic which will invoke the actual activity of choice.