How to call onCreate method from other class? - android

I want to call onCreate(Bundle cicici); from other class then i am getting "NullPointerException", so please guide me how can i call the onCreate() from another class.

There is only one way in which onCreate can be called, by starting an Activity, since onCreate is as part of Activity life cycle.
startActivity(new Intent(presentActivity.this, NextActivity.class));

if you want to call onCreate in order to actually present a new screen, then you need to create a the new Activity using the android framework style.
Ingredients:
1- An event to call your new activity( ie. onClickListener of a Button or list triggered)
2- On the event you need to create an Intent with the reference of the current activity and a class reference of your new Activity, example:
Intent intent =new Intent(CurrenActivity.this, MyNewActivity.class);
3- You need to call this activity depending on what you'll need you use startActivity or startActivityForResult, the last is use when you expect a response from your activity.
You can also refer to Android documentation Common Task, let us know if its helpful

It depends what you want to do in the second activity. If you want to create a simple task you can always use dialogs and you can show them inside your activity.
Or, on a second thought, you can hide some of your views and enable others but I guess that's not an orthodox solution :)

Related

How can I use Intent to access a method from another activity?

I am using Android Studio and have a method in my MapsActivity that I need to access from another activity. I think using intent is the best way, but I do not know how to.
This is probably a bad idea. The activity which you would like to invoke the method could be stopped while you are on the second activity, for example.
If you need the method, instantiate the class. If the method is refreshing some view on it's activity, you could maybe use "onActivityResult". Otherwise you could create an abstract class and call the method.
You can do what you want using onActivityResult.
When you go back to the first Activity you would call startActivityForResult. Then you put the data in a new intent when the user clicks the marker and finish similar to:
Intent returnIntent = new Intent();
returnIntent.putExtra("result",result);
setResult(Activity.RESULT_OK,returnIntent);
finish();
and then handle the result in your second activity.
More details are here:
https://stackoverflow.com/a/10407371/4888701
EDIT: Based on your comment, and as Felipe Martins pointed out,startActivityForResult() can work for you.
Use bundle.putDouble to add lattitude and longitude to the bundle and retrieve it later in onActivityResult().
Use this for reference: https://stackoverflow.com/a/25642483/4941959

Call activity direct from current activity

Can i call second activity from current activity without use intent? and why use intent, why not call second activity direct from the first activity?.
What does intent do in android?
Because to start any new Activity Android must have to go through the life cycle of an Activity. So it is necessary to use intent.
http://developer.android.com/reference/android/content/Intent.html
you can't ,see this link android is designed in this way to launch another activity
Intent says to Android you need open a new activity. Always when you need open a new Activity, you need "alert" the S.O before. Because of this we use Intent.
Activities doesn't work like a simple class, when we just instantiate.
This link will help you
http://www.vogella.com/tutorials/AndroidIntent/article.html

how to make the Fragment Refresh itself, from another activity

Every time I want to make my fragment call it's onStart() or onResume(), I do the the next :
getFragmentManager().beginTransaction().replace(R.id.containero1, new Massenger_frag(conversationID)).addToBackStack(null).commit();
is this good for the performance, is there another way to tell the fragment the new information arrived, and it must refresh its UI.
You can keep a reference to a "Massenger_frag" object that was used when doing the fragment replacement. You can also define a method "refresh" in your "Massenger_frag" class and call it from that instance variable. onStart()/onResume() from your fragment would also call the same refresh method to avoid code duplication.
What you are currently doing is not fast and adds a significant overhead as the new fragment is constructed every time and the old one gets deallocated.
the solution is simple: I just use BroadCast sender and receiver, and send the information throw Activity A to Activity B, the Activity B in its onReceive() call a function that refresh its content!
Use interface and create custom EventListeners. Check an example here

Launching an Intent outside an activity

I have an asynch task with my app which goes to a website, grabs the results from the API and appends a number of clickable textviews to an existing LinearLayout.
However I want to be able to launch a new activity when the textview is clicked. This isn't possible with the asynch class defined in a seperate file, would it be easier to define it as an inline class within the activity?
You can always pass Context to your async class.
A better approach would be to have callbacks (listeners) in the calling class for the async to call back to.
One approach is to inflate your TextViews from an XML file that declares an onClick attribute, naming a method defined in your Activity.
Do not use a context as an Activity! You will probably receive a cast error anyway. Instead, you can pass the activity as a function parameter, like this:
public void function(Activity act)
{
Intent intent = new Intent(act, newActivity.class);
act.startActivity(intent);
}
Or overload the constructor to accept the activity as a parameter. But I strongly suggest you to check you code. If you are calling an activity, you, probably, should be within another one, don't you agree? But, I Know that sometimes we have to make a few concessions, in order to make things work properly. So, use it wisely.

Can we create implicit Intent to call our own Activities

Is it possible to create implicit intent to call our own activity? If possible is it useful or better option is Explicit Intent?
please explain your situation more...
If a single Activity is there and you want to call the same activity again from this to have any refresh sort of thing...
Then its not a good idea...
All the views can be updated without calling the same activity again.
And if new view is to be generated then use another activity

Categories

Resources