Android: clarification on opening activities and buttons - java

I'm learning android development and I need a hint on the inner working of the whole thing.
I'm using the code showed here
In
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
}
I want to know what "this" is
In the guide I read:
A Context as its first parameter (this is used because the Activity
class is a subclass of Context)
What does Context class do? How is it used? Why does Activity inherit from it?
Now the main question:
If you check the whole example, they start the other activity directly from the button with the sendMessage() method. There is a way to use the onClick event listener and start the activity from there so I can do some stuff before starting the activity (like initializing some variables or so)?
And, is it really necessary editing the android manifest file by hand?
They put all the stuff in there about editing the android manifest everytime you add an activity. Do I have to do that exact thing every time I add an activity? I would like to edit the AndroidManifest.xml file more conscientiously, knowing what I'm typing in and why. In that guide all is put up mysteriously and they don't explain nothing.

I want to know what "this" is
To understand this, see What is the meaning of "this" in Java?.
What does Context class does?
A Context is the glue between your app and the operating system. It allows you to access resources on the device, such as images and databases.
If you check the whole example, they start the other activity directly from the button with the sendMessage method. There is a way to use the onClick event listener and start the activity from there so I can do some stuff before starting the activity (like initializing some variables or so)?
android:onClick="sendMessage" in the XML for the button is a listener for the OnClick event. You can do whatever you wish in this method, including initializing variables.
And, it's really necessary editing the android manifest file by hand? They put all the stuff in there about editing the android manifest everytime you add an activity. Do I have to do that exact thing everytime I add an activity?
Yes, every activity must be registered in AndroidManifest.xml with an <activity> tag. At this point, it is probably unimportant to understand all the nuances. I suggest following the examples you see when you want to add more activities. Note that typically only one activity will have an <intent-filter>. Don't worry too much about these until you need to learn about them later.

Related

Passing data though multiple activities (while one gets destroyed)

I'm building my first Android app in Android Studio (with Java) and I need some help understanding how to pass data through multiple activities.
The setup of affected classes/activities is this:
MainActivity: Opens a dialog called AddDialog (by creating a new Instance of it).
AddDialog (extends AppCompatDialogFragment): A dialog that has some buttons. One of them launches a class called BarcodeScanActivity (using intent).
BarcodeScanActivity: Simple activity that scan QR codes.
I want to pass the list of QR codes scanned by BarcodeScanActivity (I store them in a String Array) to MainActivity in order to use them, although BarcodeScanActivity is launched from the dialog that gets destroyed once buttons are clicked. Because of that I'm unable to set some startActivityForResult on the dialog and chain the result (onActivityResult) back to the MainActivity.
Also since MainActivity launches the AddDialog by creating an instance, I can't set a startActivityForResult there too.
I have tried adding Intent.FLAG_ACTIVITY_FORWARD_RESULT while launching BarcodeScanActivity from AddDialog, hoping that the result from BarcodeScanActivity will be forwarded back to MainActivity where I have created an onActivityResult method since AddDialog gets destroyed, but I don't know if that is even supposed to work as AddDialog is "launched" by a new instance of it, and not by using an intent.
I have thought about using broadcasts as a last resort, although I read that they are insecure, unreliable and not supposed to be used for passing that kind of data.
Any help is highly appreciated! Thanks

Changing activities with Intent on listview button

In the application I am building I have 2 listviews with, among other elements, 2 buttons on every row, one for eliminating that row and the other to pass to another activity using an Intent.
I am detecting clicks on both buttons by setting click listeners on both buttons on the getView method of the Adapter class.
The first button was something I couldn't figure out, because I needed to identify in which listview the button was clicked and the position of the clicked row which I couldn't realize how to do.
The second one I thought would be easier since all I needed to do was Intents.
However I needed to call a method from the activity class (had to instance it) since I couldn't make Intents in a non-activity class.
This last one threw a NullPointerException.
Will post all code & logcat in a while, be right back, any possible help will be appreciated.
I will need to see your code, but basically what worked for me for being able to call intents within the code of the adapter was to use the host activity.
Something like this:
item_new = (TextView) convertView.findViewById(R.id.my_view);
Activity host = (Activity) item_new.getContext();
Intent intent = new Intent(host, MyActivity.class);
host.startActivity(intent);
If you post the code maybe I can give more details
I'm not figure out the entire problem, but if you want to start an Activity from an Adapter you could simply pass the current activity to the adapter constructor (or cast the getContext() to Activity, if you used that as context of your adapter)
In a line of code:
Activity.class.cast(getContext()).startActivity(intent);

Launch the same activity for different buttons with different resource

I am new to android and was starting out by making a list of button.i want when each button click launch a same activity for them with diffrent resource such image and raw file
what is big deal here?
call same intent twice :)
refer how to start new activity here
You can the same activity/intent multiple times, just if you want the code in said activity to do something else you might want give it some extra information by using intent.putInt(<Key>, <Value>); and others

Start android program with settings

When my program start I would like to do some settings before is really starting. Forexample choose the user, check the updates and so on. After these settings I would like to start the main program with the appropriate.
Which is the best way to do this?
You can run an AyncTask, or multiple if you need one for each check, in your onCreate() and show a ProgressDialog while the data is being fetched then cancel it in onPostExecute() and move on to the rest of the MainActivity depending on the data that is downloaded. If you need help getting started with AsyncTask you can see this SO answer on the basic structure.
If you use a ProgressDialog then the app will still start but the users will see something and know that data is loading so they won't feel like it is freezing or taking too long to load (or at least they will know why it isn't loaded right away).
AsyncTask Docs
Edit after comment
For what you said you want in your comment you can do this easily with an Activity that has a Dialog Theme. This will give you the functionality you need (a couple Buttons and store the values) but it will look like a little popup. You can't use an actual Dialog as they need an Activity, the same with any menus, AFAIK. Just create your Activity and make it the launcher and main using the Intent-filters then also add the following line to that Activity's tag in the manifest
android:theme="#android:style/Theme.Dialog"
This approach should give you what you need
There are numerous ways to do that.
First - your app is doing some heavy stuff and this may be freezing user interface. In that version do:
1. Create and activity on what you will override onCreate method and set some content with a spinner - so something will be alive and user will see that something is being done.
2. after you will compute all the things that your app need and may I suggest write it to some global variables override onStart method in what change layout to what suit you and give a user a great UI!
Second - you app is not heavy lifting here just throw everything into override of onStart method.
Handy material here for educating:

i want to know how to navigate from one XML page to another in Java AWT on button click

i am making an andriod application in which i need to go from one page to another on a button click. i have tried several things but nothing worked out.
Okay, so given these are different .java files, and each has it own Activity (so, different Activities) what you want to do is call an intent as such:
Intent myActivity = new Intent(class1.this,class2.class);
main.this.startActivity(myActivity);
If its in the same Activity, (which I dont recomend) just call setContentView() again

Categories

Resources