I have the below code:
Intent myintent = new Intent (ScreenOne.this, ScreenTwo.class);
startActivity(myintent);
ScreenTwo consists of a list of phone numbers.
Both the screens extend BaseScreen.
What I need to do is click on a phone number and make a call from within my app.
I have the code right, only problem is the call screen is behind ScreenTwo.
I am guessing it is something to the with the context that I am passing in the Intent.
I have used
ScreenOne.this,
this,
getBaseContext(),
getApplicationContext(),
getApplication and
getParent(),
all to no avail.
ScreenTwo itself is supposed to be an Activity since it extends Activity. You should create some more new activities (and register them in AndroidManifest.xml too) and then call them as you are doing in your first approach.
There was no light shed on this, and all the contexts don't seem to work in bringing the screen to the top, I guess I'll have to find another workaround.
Related
I know of two ways to start an Activity with an intent. Let's say I'm in Activity A and I want to start Activity B. I could do the following.
1)
In Activity B I have some static method:
public static Intent newIntent(Context packageContext){
Intent intent = new Intent(packageContext, ActivityB.class);
return intent;
}
And from Activity A I can call:
startActivity(ActivityB.newIntent(this));
2) The other method is the one I see more often:
From Activity A I do the following
Intent intent = new Intent(this, ActivityB.class);
startActivity(intent);
Are there any benefits or drawbacks to using one versus the other? I think method 1 is a bit cleaner because it keeps the intent information in the class that will actually be started with the intent.
case first
Pros :
This follows the DRY principle mean don't repeat your self
Cons :
It is only limited to one class i.e ActivityB.class
Unclear naming convention for a Utility class
Note flexible to add extra properties unless method definition is modified to accept some map or something
Second case
Pros:
More flexible as any activity can be started
Any attribute can be added to intent object i.g putExtra and so many other
Cons :
Does not follow the DRY principle
Inefficient when duplicated many times
Improvements
Give your method proper naming
Can be overloaded to accept a map for key-value
Apply Class<?> to accept any class as parameter
The two approaches do exactly the same thing.
I think the first approach is indeed easier to understand and it's less code. But I think most people got used to the second approach and might be a little confused when they see it like that. This drawback isn't that significant though I don't think.
The thing is, if you use the first approach, you still need to use the second approach if you want to start an activity that you didn't create because you can't just add a static method to an already compiled .class file. This might make your code a little inconsistent.
Also, the name newIntent is kind of confusing. I don't know if it's just me, but doesn't it sound a bit like that you are going from Activity B to A? Maybe just intent?
I have a simple flashlight application and I would like to have the light switch on-off functionality by simply clicking on the widget also.
Now when I click on the widget my program starts, it is ok but it would be better to reach the functionality of my app directly from the widget.
I think now the appwidgetprowider just starts my activity:
Intent intent = new Intent(context, MainActivity.class);
In my flashlight`s main activity I switch on the led of the phone by the
switchOnTheFlash()
method.
Does anyone have an idea, how could I start this method from the widget?
Thank you and best regards!
Intent is not needed to turn on the flashlight as it will always take you somewhere in activities or actions. The simplest thing to do is make your switchOnTheFlash() function static like this -
public static void switchOnTheFlash() {
// Your Function
}
and Now you can call this function from any activity just like -
YourACtivity.switchOnTheFlash();
If Errors Occur --
Make sure that all the variables you use in switchOnTheFlash() should be static too or you will be getting error like cannot use non-static variables in static function (or may be somewhat written a bit different).
Another error, if you use functions like getResources() you can get errors. To get rid, Just make a static Context mContext; globally and initialize it like mContext = this; in OnCreate. Then simply replace mContext.getResources()... everywhere in your function switchOnTheFlash().
Cheers!
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 :)
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
I have activities that are create and launched from menu options. However Ive noticed that this may mean that sometimes there are two or more copies of the same activity. So Im wondering if there's a way to see if another activity is already instantiated and then have the application switch to it or create a new one if its not instantiated.
You can control some aspects of this with android:launchMode on the activity.
Programmatically try following:
Intent intent = new Intent(contextActivity, NextActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
contextActivity.startActivity(intent);
You can specify information regarding that in the android manifest. See activity element documentation. I believe that launchmode might control what you are after.