Android App starting activity_main.xml - android

I made an android app but I wanted to start a new activity (.xml).
Normally when an app starts it shows direct the activity_main.xml layout file.
I don't know how that is going on so what's the code for this because so I know how to start a new activity.

Simple u have need in main activity :-
{
(R.layout.main_activity);
Intent intent =new Intent(getApplicationContext(),AnotherActivity.class);
startActivity(intent);
}
and then you have create `AnotherActivity.class
and insert another layout for AnotherActivity in (R.layout.anotherlayout);
that solve

You can specify the layout you want to show in your Activity using setContentView method:
setContentView(R.layout.layoutForYourActivity);
This has to be specified inside your onCreate method

Create a new activity through New -> Other -> Android -> Android Activity
A activity is a .java file, the .xml is only the layout.
Call the new activity with
intent = new Intent(main.this, YourActivity.class);
startActivity(intent);
I suggest you to do some basic tutorials.

Related

Activity trigger more than once if using launchMode="singleInstance

I have a activity which i declare as launchMode="singleInstance in Manifest file. and using blow code to starting this activity:
Intent intent = new Intent(this, LockScreenActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
To close this activity i am doing like: finishAffinity();
But some how when i finish this activity i can see another open activity instance of this. Can someone tell me how i can finish all OR only create a single one when i need to start this activity from different places inside my application?

how to add a button in the running activity [duplicate]

This question already has an answer here:
Which is the best way to add a button?
(1 answer)
Closed 7 years ago.
I am very new to android development and I am having problem displaying a new button to a activity which has already run. I am using this tutorial http://developer.android.com/training/basics/firstapp/starting-activity.html. Here they display Hello World in the new activity. I would like to add a button called "SKIP"! So that the user can go to a another new activity called Functions. How do I do that with the intent? My intent is already being used to display the string hello world! I am using android studio.
Appreciate any help thanks
To create a button you need to go to your main XML file, add a button. Then in your Activity create a onClick listener for the button.
When it comes to going to another activity you need to create a new Intent with
Intent intent = new Intent(this, Functions.class);
startActivity(intent)
You would call your new activity with the following snippet within the OnClickListener of your button:
Intent intent = new Intent(this, Functions.class);
startActivity(intent);
And define your Functions class in your AndroidManifest.xml file similar to what the originally defined Activity is defined as. But you should only have one launcher Activity.

Android create new instance of current activity

I an have activity called A in my project, there is a button that create new instance of the same activity. For example i want to do something like this:
intent = new Intent(this, A.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(MainActivity.EXTRA_DATA, data);
startActivity(intent);
If i use this code the app crashes when i push the button.
I have found the solution. The activity use a object of another class called data, this class need to implement Serializable.

Can we invoke one activity into another activity?

I Mean that i want to use one activity into another activity, Like class using create instance of that class. Is it Possible?
Well, I think you should use Intents to call an activity from another activity.
Call this from your Activity:
Intent in = new Intent(getApplicationContext(), NextActivity.class);
startActivity(in);
you can do it only by saying startActivity(), no other go. you can't make an instance of Activity because , an Activity gets created when its onCreate() method gets called, but when you say new MyActivity() its default constructor is called and not its onCreate() method (which Android OS will not accept). so always say startActivity() or startActivityForResult() which are handled by android OS
Write this code from where you want to run activity
Intent intent = new Intent(current_Activity_name.this,New_Activity_name.class);
startActivity(intent);
And add the following code into manifest file
<activity android:name=".New_activity_name" />
Well, since an Activity is a displayable-window, the appropriate concept would be that one Activity can be "launched" from another. This is how you achieve that:
Intent i = new Intent(CurrentActivity.this, NewActivity.class);
CurrentActivity.this.startActivity(i);
This code snippet can launch NewActivity from any point in the CurrentActivity code, for example, an 'OnClickListener'.
Yes, it is possible. This is achieved through Intents.
Intent intent = new Intent(this.getApplication(), TARGET_ACTIVITY_NAME.class);
//To add data use intent.putExtra(NAME,VALUE);
intent.setData(data.getData());
try
{
startActivity(intent); // This ll launch the TARGET_ACTIVITY_NAME
}
catch(Exception e)
{
}
For more information refer this link.
Shash

How to navigate to another page in android?

I'm new to android. Please tell me how to navigate to a new page in android.
Thanks in advance.
Edit:How to start a new activity from an existing activity
In android to navigate to another page means you have to start another activity. for starting a new activity use this
Intent intent = new Intent(currentActivity.this, nextActivity.class);
startActivity(intent);
You should mention the next activity in the AndroidManifest file.
To change your page (i think you're refering to "Activities" in android you need to issue a so called "intent").
You cann do that by:
startActivity(new Intent(nextactivity,NextActivity.class));
or
startActivityForResult(new Intent(nextactivity,NextActivity.class),1);
if you want the new Activity to return any result to the activity who started the new one.
But what i recommend is, that you read this documentation here:
http://developer.android.com/guide/topics/intents/intents-filters.html
and come back to this question if you have need additional assistance on concrete problems on that topics.
i hope that helps.

Categories

Resources