Nested inner Activity class in android - android

Is declaring a class that extends Activity inside another Activity class possible? If it is, how would I register that class in the manifest? Also, is that something that can be reasonably done or is it a bad idea?
I was thinking of something like
class ListClass extends ListActivity{
...
ArrayList items;
class ItemClass extends Activity{
...
Item item;
#Override
onCreate(){
Integer pos = getIntent().getExtras().getInt("pos");
item = items.get(pos);
}
}
#Override
onItemClick(int position){
startActivity(new Intent(this, ItemClass.class).putExtra("pos", position));
}
}
Note the syntax isn't 100% correct obviously, mostly pseudocode.

Yes, it does work -- it is just another class -- you just need to declare your activity using inner class notation in AndroidManifest.xml:
<activity android:name=".ListClass$ItemClass"/>
Seems to work fine for me, but perhaps when this question was asked, it wasn't supported in older versions of Android?
Not sure WHY you'd want to do this, but you can.

No, that's not possible. After all, the Android operating system will need to instantiate the Activity if it is started at any point (say, if you start it through an intent), and it's impossible to instantiate an ItemClass without a parent ListClass.
Remember that each Activity is completely independent and can be started at any point through an intent.

I'd also be curious why you'd want to do this.
However, I don't see any reason why it wouldn't work. Couldn't you reference it in the AndroidManifest as you normally would as long as both classes are public? i.e. com.falmarri.ListClass.ItemClass?
Edit: Nevermind, this doesn't work as EboMike pointed out.

Related

How to access activity resource from another activity?

I want to change textView(in mainActivity)'s property like textSize, or textColor.
Then I tried to use it at setting activity.
View view = getLayoutInflater().inflate(R.layout.activity_main, null);
readTextView = view.findViewById(R.id.textView);
And It doesn't work.
Also, I tried to How to update a TextView of an activity from another class this answer. But isn't it can only change the text? If I need to change much property, I have to make a method in my activity.
Android access main activity variables from another class
I referenced this answer.
Declare public static the resource which you want to change.
Use like [Your Activity].[The Resource] at your setting Activity.
I really sorry to question like this... Sorry.
You must not using a public variable as a mechanism to update your View inside an activity because of the following:
You can't ensure that the activity is always exist. There is a probability that the activity is killed by system because of error or you're finishing the activity.
You're coupling your activity with another class. So, each time you're changing the activity there is probability that your change propagate to another class. This probably will introduce bugs to multiple classes.
You better strictly access the View from another class by sending only the view as a parameter. For example, if you have the following activity:
public class YourActivity extends AppCompatActivity {
private TextView mTvName;
...
}
to change the properties of mTvName, you need to create something like this:
public class TextChanger {
public static void maximizeTextSize(TextView tv) {
tv.setTextSize(30);
}
}
then you can use it in your Activity:
TextChanger.maximizeTextSize(mTvName);
If you want to update the TextView from another Activity, you can use startActivityForResult for starting the another Activity. Then you need to receive the result to change your TextView by overriding onActivityResult in your activity.
In case you need to update the TextView without any coupling with another class or Activity, you can use Event Bus mechanism. You can use EventBus library.

Android: How to call a a Class within the Current Class

I'm working on an application that has multiple Activities, and I'm tired of running back and forth between the Manifest and XML layout and stuff. Is there a way to have
Intent intent = new Intent(MainActivity.this, MainActivity.Settings.class);
Or something? Because I've tried it, it doesn't throw me an error, but it just force closes the application. I'm able to bundle all my classes from different .java into one, for ex.
public class MainActivity extends Activity
{
...
#Override
protected void onCreate(Bundle MainActivityState)
{
...
}
public class Settings extends ...
{
...
}
public class Register extends ...
{
...
}
public class Login extends ...
{
...
}
public class BeautifulLady extends personality ...
}
Simple. Just don't even try.
An activity loosely represents a single screen - something the user interacts with. Android is built around this concept and trying to circumvent it will lead to tears.
Stick with it. Having your classes in separate files, and having layout XML separate for each activity, will become your friend and will actually speed things up once you are familiar.
Start with the Activity life cycle document and read it several times until the penny drops. Then expand out from there.
http://developer.android.com/reference/android/app/Activity.html
Object oriented programming, with classes that take care of themselves, is a joy and regardless of which platform you choose to develop on is the way to go for the foreseeable future (old hands, no debates on OOP vs functional please ;)).
If you are going to do mobile development, then the separation of activities, classes and UI is the same concept, just done differently.
See also MVC programming and its' cousins.
http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller
Good luck.
Perhaps you can define your Activity as 'Single Top', then launch your activity from herself like MainActivity.this.startActivity(new Intent(MainActivity.this, MainActivity.class). It'll then go into onNewIntent() and you will redisplay what you want to redisplay. This way you will have only one screen.

How to handle Android Java objects which "extends Activity"?

Let's say I have two classes:
Class Show is my normal Android activity.
Class Work is a Java class which does a lot of work:
public class Work extends Activity {...}
As you can see, Work extends Activity. Thats because it needs some methods that are only in Activity (I don't mean methods which regard the UI).
In my Activity Show I make an Objekt of class Work
protected void onCreate(){
Work mWork = new Work();
mWork.doSomething();
}
My question:
How shall I handle my object Work regarding its lifecycle? Is it like a normal Java object and I don't have to care about its lifecycle, or is it like a normal activity and i have to call finish()? I am confused because it's kind of both.
Based on our discussion in the comments, the answer here would be to pass the Context of your current activity to the target class, and use the accessible methods through that context instance to do whatever task it is you want, as the Activity class basically extends ApplicationContext.
For example (in a class not extending Activity) I can add:
myLocationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
and use myLocationManager as I please. Also, this way the object created locally would be set for garbage collection upon destruction of your main Activity.
Thought it'd be worth having an answer others could refer to rather than scrounging through the comments.
you should read this to understand activity lifecycle.

android.app.Application invoking ArrayAdapter.notifyDataSetChanged

I have a static class which extends android.app.Application (HireApplication) which is doing a lot of my background work. I create AsyncTasks here, I store the largest data objects that my application uses. I'm writing this code in the Application class so that I can reuse it between separate activities for Phones and Tablets.
This feels like the right way to layout my app, but now I have a problem. An AsyncTask has finished and needs to run notifyDataSetChanged on an ArrayAdapter within a Fragment (ListFragment).
Here is my solution:
ListFragment:
public void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myListAdapter = new MyListAdapter(getActivity(), HireApplication.myList);
HireApplication.myListAdapter = myListAdapter; // Tell the application where the ListView is so that it can notifyDataSetChanged
}
HireApplication:
public static ArrayAdapter<BikeStation> stationListAdapter = null;
private static void asyncTaskCompleteCallback() {
{...}
myListAdapter.notifyDataSetChanged();
}
Now, this works... but it doesn't feel right. I am giving the Application a reference back to a UI element which sounds like it's a bit tightly coupled, but I don't know a better way to do this.
Is there a better way of my Application notifying my ArrayAdapter or Fragment that it should take some action to update the UI with fresh data?
Cheers.
The whole thing looks wrong to me... if you are using fragments and adapters, you should be using LoadManager instead of AsyncTasks. Also, putting everything on your Application looks like a poor solution to the problem you want to solve; it will lead to posible memory leaks and does not feel right.

Multiple activities within a class?

So what I want to do is this. I want to have a class that contains other classes which start activities, but I'm not sure if its possible, or even a good idea. An Example:
public class General{
public class Activity1 extends Activity{
//Start Activity
}
}
Is there a way to call such an activity?
So the solution I chose to go with was to use packages. After reading about it here http://developer.android.com/guide/topics/manifest/manifest-intro.html and here Android: Including multiple Java Packages to Manifest , it seems like a better method to do what I stated above.

Categories

Resources