how to call an object of another class - android

I have a main class which has edittext box. And i have another class in the same source folder. Now i want to use the text typed in main class accessible to the another class.
any help is greatly appreciated

There are different approaches:
1) You could store the value in a static variable somewhere globally in your app (when you leave the activity = onPause) - works ok if your app activities run all in the same process, which by default is the case - unless specified differently in your manifest
2) or use the SharedPreferences (ref. to android dev docs).
3) If your one activity starts the seconds one right after, you could pass the value as an intent extra (ref. to android dev docs):
Intent intent = new Intent(this, A.class);
intent.putExtra("EXTRA_TEXT_TO_REMEMBER", editText.getText().toString());
startActivity(intent);

Related

Which function does intent have? [duplicate]

This question already has answers here:
What is an Intent in Android?
(14 answers)
Closed 8 years ago.
I dont know much about intent (or android) so.. Can someone please explain me what is it exactly? i have search on the internet, A LOT.
Also what does each line of this code do?
Intent intent = new Intent (this, DisplayMessageActivity.class);
intent.putExtra("a", "b");
Thanks in advance
I suggest reading Android Intents
You couldn't have search for very long, since this is very basic topic.
I suggest you read more of Android's API guides.
Line 1 = Create message that describes what to do, in this case start "DisplayMessagActivity"
Line 2 = Add content to the message
Intent intent = new Intent (this, DisplayMessageActivity.class);
For this line, its function is to create a navigation from the current activity/page to the displaymessageactivity page.
it is like from here to there.
For this intent.putExtra("a", "b"); the purpose of this is to put like a temp storage/variable to pass to the next page for retrieval. In this case, you put the value "b" in the variable "a". With this method, you can use the value on the other activity or page.
All the above are just storing of info, it is not executed yet. if you want to execute the intent do the following
startActivity(intent);
The best example to state the behavior of Intent is it behaves like a POSTMAN that delivers message to the stated address.
Whether it may be calling service ,BroadCastRecivers ,Activity they are used in number of occassion.
Intents are asynchronous messages which allow application components
to request functionality from other Android components. Intents allow
you to interact with components from the same applications as well as
with components contributed by other applications. For example, an
activity can start an external activity for taking a picture.
Intents are objects of the android.content.Intent type. Your code can
send them to the Android system defining the components you are
targeting. For example, via the startActivity() method you can define
that the intent should be used to start an activity.
An intent can contain data via a Bundle. This data can be used by the
receiving component.
Intents can be used to start Service, call Activty, call Sub Activity, transfer the data between Activity or retrieve the data from Activity

Android - Call Main Class Activity

I would like to go back to the Main Activity at a certain point in my application. I tried calling the Activity in an Intent as usual, however since the Activity name for the main class is called "android.intent.action.MAIN", which is the general name of every application's main activity, a "Complete Action Using:" menu pops up with every possible application on the phone as an option. I do not want this; what I want is that the main activity for my application loads up.
How can I achieve this?
Thank you in advance!
You can also specify a specific Activity to launch like so:
Intent nextActivityIntent = new Intent(this, MyActivity.class);
startActivity(nextActivityIntent);
As per the Intent documentation, the ACTION_MAIN flag is used to specify that you want to launch an application using the application's main entry point. You generally do not use it in your app unless you are trying to open another app.

Is there any difference between these two types of intent declarations?

step1 > Intent i = new Intent(home.this, secondactivity.class);
step2> Intent i = new Intent("android.intent.action.secondactivity");
I am a little confused as to what difference does it make when creating an intent like in
step 1 or step 2
In the 1st one I am specifying the current instance of the class from which the activity should go to the next activity e.x on a button click in the home activity then it will go to the second activity.
In the 2nd one I am not doing that.
Does it makes any difference or both are same ?
They are different:
In the first variant you explicitly say which component/activity to execute, so it's guaranteed that you will execute that component. In that case your second activity doesn't even need to have an intent-filter specified.
In the second variant you specify an ACTION name. For that to work, your second activity must have an intent filter with the same action. However note that if by any chance there is another application with an activity which has an intent filter with the same action name, then when launching the activity, the user will get an activity picker dialog to choose which one to use.
You should use the first variant if you ALWAYS want to execute your second activity on button click.
You should use the second variant if you want to create an API, i.e. you want other applications to be able to hook into your application workflow.
You can use step2 when you call your SecondActivity from somewhere outside. Let's say from a broadcast Receiver, or from a Service.
Your second intent is wrong, "android.intent.action.secondactivity" is not an action.

android switchstatement question

I was wondering, how would I make a switch statement, that when that certain case was triggered, it would open a new screen with text. Would I use an intent? And if so, which one?
Thank you for your help in advance.
When you want to open a "new screen", you probably want to open a new activity. You would create a second Activity-derived class and use the following overload of the Intent constructor with startActivity:
Intent intent = new Intent(this, MySecondActivity.class);
startActivity(intent);
this will explicitly attempt to open a new Activity with the class name MySecondActivity
to pass a String of text from one Activity to another in this way, you can add it to the intent.
String someValue = "Some Value";
intent.putExtra("Some Key", someValue);
and in the code of your other Activity, you can get at this string via the Intent
getIntent().getStringExtra("Some Key");
Obviously you want to do null checks to make sure the key exists in the Intent, and you want to put a proper constant String somewhere instead of using a literal String for a key, but this is the basic gist.
Kriem... As Rich said you can launch a new activity using intents and push data to the new activity using extras. You can push data back to your main activity using startActivityForResult instead of startActivity. You can return to your main screen by calling finish() in the new screen. Finally, you can put the new screen event handlers into the NewScreen.java file.
The overall effect is a near full separation from dependency between the two activities, so that you might be able to easily re-use the NewScreen.java class in another project. I have some code here.

How can I launch an activity in another apk?

HI,
I have 2 projects (each has its own apk).
Can you please tell me how can I launch an activity which is in another apk that I created?
I have this activity which I want to launch from another project:
// what should I put in here so that I can launch this from another activity in another project?
You would have to implement an Intent interface. I.e. have your activities respond to specific Intents specified via Intent-filters in your manifest. Have a look at this page:
http://android-developers.blogspot.com/2009/11/integrating-application-with-intents.html
Intent myIntent = new Intent();
myIntent.setClassName("com.activity1", "com.activity2");
startActivity(myIntent);
Activity 1 is the activity that is already running.
Activity 2 is the com name of the activity you want to launch.

Categories

Resources