android-context

Use application class as singleton

做~自己de王妃 提交于 2019-12-11 01:40:56
问题 In Android we often have to use Context classes. When a class or method requires a Context we often use Activites or Services for such arguments. The following code is from a website I found but I do not know if this is good practice and if it is okay to use this solution: public class MyApplication extends Application { private static MyApplication singleton; @Override public void onCreate() { super.onCreate(); singleton = this; } public static MyApplication getInstance() { return singleton;

Unable run ProgressDialog - BadTokenException while showind

半城伤御伤魂 提交于 2019-12-11 00:20:04
问题 I am having a problem with a ProgressDialog thing in MVVMCross. I am getting Android.Views.WindowManagerBadTokenException : while creating ProgressDialog via IReportService where I have context from setup.cs. public class Setup : MvxBaseAndroidBindingSetup { public Setup(Context applicationContext) : base(applicationContext) { } protected override MvxApplication CreateApp() { return new NoSplashScreenApp(); } public class Converters { public readonly MvxVisibilityConverter Visibility = new

How to show Dialog from a static method

谁说我不能喝 提交于 2019-12-10 18:14:22
问题 In my game, which is done for both Android and IOS using cocos2dx , I have to show video(for Android). I am planning to show it in Dialog(on top of game view). Problem is that, I don't have any Activity referenced to show Dialog(as Dialogs can only be shown in Activities). Even though, In cocos2dx lib folder, there is a Cocos2dxActivity but I am not getting how to make use of it. From C++ code, I am calling a static method from Java class as below void LMJNICommunicator::showVideo() { LOGD(

Android toast.makeText context error

时光毁灭记忆、已成空白 提交于 2019-12-10 17:53:33
问题 I am having trouble calling toast.Maketext inside of a location listener. The context is not available, what am I doing wrong? private LocationListener ll = new LocationListener() { public void onLocationChanged(Location l) { // SMSReceiver.l = l; String s = ""; s += "\tTime: " + l.getTime() + "\n"; s += "\tLatitude: " + l.getLatitude() + "°\n"; s += "\tLongitude: " + l.getLongitude() + "°\n"; s += "\tAccuracy: " + l.getAccuracy() + " metres\n"; s += "\tAltitude: " + l.getAltitude() + "

best way to use context in fragment

情到浓时终转凉″ 提交于 2019-12-10 16:16:59
问题 i m using fragments in my application. i created one Parent Class called BaseFragment and all other fragment extends this Basefrgment below is the snippet of this Basefragment BaseFragment.java public class BaseFragment extends Fragment { public MainActivity activity; @Override public void onAttach(Context context) { super.onAttach(context); if (activity == null && context instanceof MainActivity) { activity = (MainActivity) context; } } } public void replaceFragment(Fragment fragment,

Calling TextToSpeech from a different class

ぐ巨炮叔叔 提交于 2019-12-10 12:12:38
问题 I am trying to call TextToSpeech in a different class. Here are what my classes look like right now: //MainActivity.java public class MainActivity extends AppCompatActivity implements View.OnClickListener { private SpeechRecognizer sr; sr.setRecognitionListener(new Listener()); } //Listener.java public class Listener implements RecognitionListener() { public void onResults(Bundle MainActivity theMainActivity = new MainActivity(); //the following line always breaks the code: tts = new

Difference between context [duplicate]

ⅰ亾dé卋堺 提交于 2019-12-10 11:32:24
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Android - what's the difference between the various methods to get a Context? I want to know what's the difference between using this , ClassName.this , getApplicationContext() or myContext ? What are the effects of using each as context in the Toast below? public class ClassName extends Activity { final ClassName myContext = this; ... public void onCreate(Bundle savedInstanceState) { ... button

Android restart my activity

回眸只為那壹抹淺笑 提交于 2019-12-10 10:32:09
问题 How can I do that? on button click: mycontext.finish(); and then: start again? 回答1: You could try either this: MyActivity.finish() Intent intent = new Intent(MyActivity.this, MyActivity.class); startActivity(intent); Or if that doesn't work, you could do this: private boolean isRestarting = false; ... // When button is pressed isRestarting = true; myactivity.finish(); ... // in the onDestroy() method if(isFinishing() && isRestarting){ Intent intent = new Intent(MyActivity.this, MyActivity

Is each activity in android having its own context? What is the scenario with multiple activities in single application

╄→尐↘猪︶ㄣ 提交于 2019-12-10 10:04:47
问题 I was reading this link to understand context in the android. I still have one question that does each activity have its own context?. Please consider i am an android learner in beginning level. 回答1: I still have one question that does each activity have its own context? Each Activity is its own Context . Activity is a Java class; it inherits from Context : 来源: https://stackoverflow.com/questions/34269458/is-each-activity-in-android-having-its-own-context-what-is-the-scenario-with-mu

How to start an activity from a dialog in Android

谁说我不能喝 提交于 2019-12-09 02:59:29
问题 I created a custom dialog and I'd like to start a new activity when OK is clicked. How can I get the context to set it as first argument of my Intent constructor? I can create the intent using getContext() , but I can't call startActivity . Shall I pass the activity calling the dialog to the dialog's constructor? Is it the usual way to start an activity by clicking a dialog? public class CustomDialog extends Dialog implements OnClickListener { Button okButton, cancelButton; public