Launch the same activity for different buttons with different resource - java

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

Related

Everytime goto Activity increase instance counts in AndroidDevMetrics in android

I have added AndroidDevMetrics library in my project which has many activities and screens.
My app working fine and no lags also no frame drops u can see from image i will add below.
The problem is that; the instance count for each activity got increased as i go to that activity again and again. You can have better view from image attached below. I went to ArtistDetailsActivity 5 times from HomeActivity. and only 1 times for some other screens.
I Als have tried some ways to remove old instances of activities. Not worked.
Niether the number of instances are restrained to 1, Nor the app is creating any problem with that.
My Questions are:
Is this normal behaviour? (About multiple instance of activity as we render it again).
If not normal behaviour. How can i remove old instances of activity.
Will it create any impact on my app on heavy usage or low end devices
Thanks for reading uptill here.
I appreciate any help or direction in this regards. Thanks
If you want to retain a single instance of the same activity then you could use this
Intent nextScreen = new Intent(currentActivity.this, MainActivity.class);
nextScreen.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | IntentCompat.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(nextScreen);
And if you want all the activities from stack being cleared
finishAffinity();
For more information
https://developer.android.com/guide/components/activities/tasks-and-back-stack

Android: clarification on opening activities and buttons

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.

Android App Development : How do change content on screen depending on event

Just starting out developing some android apps. Coming from a web development background I'm wondering if the idea behind changing whats displayed on screen is similar to linking html files.
Say I had a button that once clicked would then display a completely new page, button gone and completely new content in its place. I had thought at first that this was done just by having conditional statements in the main activity class. But I don't see how this would work with the xml layout file.
So I have come to the conclusion that you have to define multiple xml files and switch between them using logic in the main class.
If this is correct whats the best way to go about this, and if not could some suggest how this is achieved normally?
Thanks.
I think it wise to follow the following tutorial: http://developer.android.com/training/basics/firstapp/index.html
Have you tried visiting Android developers' website?.The solution to your question can be obtained taking the Android training module in that website. You have said you want to go to a new page, you can use Activities here.
Let me explain you this in simple terms.
In Android for every page(Activity) you need to make a separate xml file. for example main_activity.xml.
And for each page(Activity) there is a java class. For ex MainActivity.java. This class may contain event handling and business logic part.
Now let's go to your question about switching between multiple pages.
Suppose you have 2 activities: MainActivity and SecondActivity.
Now in MainActivity you have a button then you set its onClick attribute to its event handling method. This can be done in xml file.
android:onClick="goToSecond"
Now in MainActivity.java you need to create a method which looks like this.
public void goToSecond(View v)
{
Intent i=new Intent(MainActivity.this,SecondActivity.class);
startActivity(i);
}
This is a code snippet for switching to second activity.
And I also agree with other answers that you should check out developers.android.com
Hope it helps.
There is no need to switch between the XML files for portrait and landscape mode. Android does that for you. If you are writing an app that uses both orientation, then you have to write separate layout files and keep them in layout(for portrait), layout-land(for landscape) folders. This is not at all necessary if your design looks same in both orientation.

Changing views/activity in Android

I am a newbie in Android and currently I am designing an application where I have faced a big problem with changing views. I have 3 classes, each one for a different screen. I use a button to change pages but it does not seem to work. Every time I move to the next screen, the variables methods etc of the 2nd screen are located in a different class. Can you please show me the simplest way to do this? Thank you.
Place each screen in different Activity. Start respective activities according to button presses. To pass data between activities use Intent.
Activity Reference
Intent Reference
Simple Example for Activities and Intents

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