I'm developing a task reminder app and still not familiar with Android Studio. The layout was an Activity Form with a floating action button that opens the second activity for inputting name, date, time, etc., the second activity has a 'create' button that supposes to add a checkbox widget to the main activity form and I was having difficulties to make it work unlike at java where you just have to add it directly on its respective container.
By the way, this is how I created the object for my checkbox...
private class task extends androidx.appcompat.widget.AppCompatCheckBox{
task me;
public task(#NonNull Context context) {
super(context);
me=this;
me.setText(taskName);
}
}
it's not correct to add a widget in another activity in real-time, there are many solutions for this case, the simplest way is to start the second activity with startActivityForResult and in the second activity, return changes to the first activity and on onActivityResult handed if there are any changes or not, if yes create your widgets based on the returned result.
Related
me and a partner are working on a project and have designed an app that uses nfc, we want it so that when you're off the app you can tap an nfc card and read it as well, the problem is we don't specifically have an activity for reading or writing a card, its triggered using a button on main activity that starts a dialog fragment, starts a class and reads the card
is it possible, using the intent filter (or any other way without having to create an activity for it) trigger a method in the activity? at the moment we just have it to bring you to the main activity but we want to start a method in main activity when that happens.
Intent filters start Activities. Activities always start with onCreate. If you want to call a single function in your activity, your code is mis-architected and that function should be in a common helper class, not in your activity.
I am trying a make notepad app so every time a new note is opened the layout will be the same. also, the number of activity(new note) should not be defined as many possible
If the activity is always the same, you should probably create an adapter that allows you to change the texts and images of your activity without the need to create several activities.
I have two Activities in one application.
First one updates its TextViews every 3 seconds. It works fine.
When the keyguard (lock screen) is activated the first activity launches the second activity which appears over the lock screen (in order to show data even if the screen is locked). It also works fine.
I would like the TextViews of the second activity to be updated periodically by the first activity. I have played hours with this and tried a lot of suggestions I found with Google but none of them worked for me. The second activity always crashes with NullPointerException at the moment when the TextView.setText() is called.
What is the best practice for doing this?
Thanks in advance for any help.
I don't think there is a good way to do this, as your first activity could get collected by the system, and you generally don't want to do work after onPause has been called.
I would move that logic that updates the views into a service that runs in the background. Since it sounds like you only need this service while the application is running I would create a bound one.
http://developer.android.com/guide/components/services.html
You can pass the data on calling another activity as :
Intent intent =new Intent(FirstActivity.this, SecondActivity.class);
intent.putStringExtra("TextName","Value");
startActivity(intent);
As Ashish said you could use EventBus.
Add the library to your app and in your Second Activity register your activity in the EventBus in onCreate method:
EventBus.getDefault().register(this);
Create a new class in your project to define an event type:
public class TestEvent {
public TestEvent() {}
}
So in your second activity create a method to receive the event:
public void onEvent(TestEvent event) {
//stuff to do
}
Now, in your first activity you just have to "fire" the event in the method executed each 2 seconds:
EventBus.getDefault().post(new TestEvent());
Each time you execute post method, the onEvent of your second activity will be run.
A way to do it is by defining a Singleton object that holds the value to be displayed on the TextView, for instance, a Integer or a String.
Both activities have access to read/write into this object. So when you come back to the second activity, maybe on the onResume() method..you can the following:
public void onResume() {
super.onResume();
textview.setText(""+ MySingleton.getInstance().getValue());
}
On the other activity:
public void updateMethod() {
int newValue = .....;
MySingleton.getInstance().setValue(newValue);
}
This will make sure that whenever you come back to this activity (as onResume() is called), the value will be updated into the TextView. Of course, assuming that you are updating the value from the other activity accordingly.
Note this is the simplest solution you can do, professionally, I would do an event driven solution, where the observer gets notified when the value is changed. For that you can play with http://square.github.io/otto/ library.
I have an app that circles around the main activity (a main menu). In each other app there is an option menu item that directs to this activity.
At first, I always started a new main activity when this item was selected. Using the intent bundle, I did tell the main activity that some initializations I do on a fresh start were not necessary.
However, I didn't quite like the overall behavior. I stumbled upon android:launchMode="SingleTask" and this seemed to help: now I don't recreate my main menu activity all the time; also, if I press the "back" button I come back straight to the home screen. This feels quite nicely like a proper "main" menu.
My problem now is this: if I run another activity of my app, press home button and then reopen my app (e.g. using "last apps"), then I don't go back to the last activity, but to the main one. The other activity is destroyed.
Any ideas how I can implement the behavior of SingleTask without only being able to return to one activity?
If your other activities are declared normally with activity defaults in Android, then going back to your app should take you to the same activity where you left off (using the hardware home button)
However remember that the Android system kills applications when it requires system resources. So your app may have been killed when you went to the other application. Then when you get back to your app, the default launcher activity will be restarted, which is your Menu activity.
To get back to the main activity from any activity, do this:
public static void goHome(Context context) {
final Intent intent = new Intent(context, HomeActivity.class); //give name of your main activity class here
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(intent);
}
That will clear the activity stack and get you back to your main activity. As you declared singleTop, it will bring the existing main activity to the foreground. The flag Intent.FLAG_ACTIVITY_CLEAR_TOP will remove all activities in the stack on top of the main activity. (I am assuming you are within the same application).
Now, all your other activities only need to include a button whose click listener invokes the method goHome();
From your main activity, if you press the hardware back button, it should exit your app.
Why not call finish() on the activities that were created by the main activity? This way you return to the main activity, without creating a new one...
I think you should save the state of you activity before starting another activity, and then resume your activity whenever you come back on last activity.
see Activity Life cycle from Android
http://developer.android.com/guide/topics/fundamentals/activities.html
I want to update textview in my main activity view.
I have main.xml file that contains the main application ascreen,
in that screen, I have textview that I need to update from time to time from another activity (class).
If I try to update those values from the main activity it work perfect, but when I'm trying to do it from deferent activity the application crashes.
Here is the method of the main activity, I need to know how to call it from the other activity.
Main Activity method:
public void update_counters(){
TextView sms_textview = (TextView) findViewById(R.id.sms_textview);
sms_textview.setText(String.valueOf(sms_missed));
}
Please Help
Indeed it's a bad practice to have any static references to activities (or other contexts). Activity are designed to be rather independed from each other.
You can receive a result from an activity that was started with startActivityForResult() method and than react appropriately.
Like #Roman said, it's a bad practice to touch the UI stuff from other activities, and activities should be independent. What you can do, is a little bit redesign your message passing method. One might be good is to use the broadcast receiver, it also guarantees a better extensibility of your program.
So, whenever your 'other' activity need to call the update_counters, it turns to broadcast such an intent. Your previous MainActivity should register to listen to that broadcast, and update the UI as necessary. What would be the best is you can have several more instance of that activity, or other activity that can register to that broadcast as well.
Is there only one instance of your main activity? If so, store a static reference to it in the main activity class, initialize it in onCreate. Then have a static method that uses that reference to get to the instance:
static MainActivity TheMainActivity;
static public void update_counters()
{
TextView sms_textview = (TextView) TheMainActivity.findViewById(R.id.sms_textview); sms_textview.setText(String.valueOf(sms_missed));
}
The in the other activity:
MainActivity.update_counters();
This is called a singleton. Or, sometimes, a global.