Hey guys, i am making an android application where i want to show a dialog box about legal agreement everytime the application starts, i have a public method showalert(<>); which shows an alertdialog by building a dialog with alertbuilder. I added a call to showalert() method on the onCreate() method of the main activity to show it, but whenever the user rotates the screen, he gets the dialog everytime. The activity restarts itself when the phone is rotated. I tried adding android:configChanges="keyboardHidden|orientation" to my manifest but that doesnt help on this case. Also can i know how to register a new application class on manifest file. I am trying to create an application class and put the code to show dialog on the new class's oncreate method. But i am not being able to load the class when the app starts.
I also checked Activity restart on rotation Android but i dont seem to get a thing. I am pretty much a newbie to android programming, could someone simplify that for me?
Any help would be appreciated. :)
you could maybe look at the onRetainNonConfigurationInstance() activity method, which is called just before destroying and re-creating the activity on screen orientation change.
it allows you to retain an object that could for instance contain a test variable to know if your legal thing was already shown or not.. example :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final String test = (String) getLastNonConfigurationInstance();
if (!("textAlreadyShown").equals(test)) {
//here : show your dialog
}
}
#Override
public String onRetainNonConfigurationInstance() {
return "textAlreadyShown";
}
Set the main activity to an activity that just shows the legal notice, when it is accepted/cleared, show a second activity ( which is currently the main activity )?
Related
In my first steps in exploring Android I now start with QR scanning.
Works all pretty well. But I am not able to come back from the ResultHandler after read the QR successfully to my MainActivity.
public class MainActivity extends AppCompatActivity implements
ZXingScannerView.ResultHandler
{
private ZXingScannerView mScannerView
....
#Override
public void handleResult(Result rawResult)
{
// my results are ok in rawResult
// the scanner does not scan anymore but it is still there
// how to go back to my main activity???
}
public void ClickButton (View view)
{
mScannerView = new ZXingScannerView(this);
setContentView(mScannerView);
mScannerView.setResultHandler(this);
mScannerView.startCamera();
}
}
}
I tried
mScannerview.stopCameraPreview
mScannerView.stopCamera
this.finish
setContentView(R.layout.activity_main); // shows my activity_main
// but I can not click anything
Thanks!!
EDIT
I added some code to describe it a bit better. The idea is from
https://www.numetriclabz.com/android-qr-code-scanner-using-zxingscanner-library-tutorial/
Your question isn't clear but I'm assuming you want to restart the scan process. Normally, you'd have to restart the SurfaceHolder to be in preview mode. Luckily for you the ZXingScannerView already has a method to do that. Call mScannerView.resumeCameraPreview(this) to restart the scan process.
Otherwise can you clarify? You say you want to go back but you're already in MainActivity
If you want to go back into activities/fragments stack you can try Activity.onBackPressed()
if you are in a fragment you must call this method against attached Activity
What do you want is not going back to your activity. You want to restore activity's layout.
I think the better choice is to add ScannerView to your activity's layout file with android:visibility="gone". Then in on click you can get this view and change it's visibility to VISIBILE.
Then when you have handled scanning result, you can reset yuoir ScannerView to visibility = GONE
I too was stuck with this problem for an hour, just like you. And later realised..
To solve this problem DON NOT implementing the ZXingScannerView in the same activity or fragment. Instead start a new activity when you click the button and this activity is just for the ZXingScannerView
Once the Scan is done finish and pass the data back to your activity or fragment
Just restart your MainActivity before this.finish()
the code below will start your main activity through intent...
worked fine for me
startActivity(new Intent(this,MainActivity.class));
this.finish();
remove from onCreate method this line setContentView(your layout) and when you finish scan write it after you stoped the camera then you can use your layout after scan
I had a bit of a look into android concepts and activities.
I put the QR handling in a 2nd activity and it worked well with the finish ().
Thanks for help anyway!!
I think it's too late, but I came with the same problem and I had so find a solution by myself.
You were on the right way, you need two steps more.
I called the methods where I link and set the listener of any buttons
There are the methods
Basically you were right where you set the content view, but you need to give the buttons their functionality back.
(I know its late, but better late than never). Good luck!
I have created a startup activity from where I am calling another activity which has a view pager and shows some introductory pages.
This app was taking time to load so I thought to display a progress dialog till the activity loads, but that progress dialog also appears few seconds later.
startup activity:
public class StartUpActivity extends AppCompatActivity {
boolean isUserFirstTime, login;
public static String PREF_USER_FIRST_TIME;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
isUserFirstTime = Boolean.valueOf(Utils.readSharedSetting(StartUpActivity.this, PREF_USER_FIRST_TIME, "true"));
Intent introIntent = new Intent(StartUpActivity.this, SlidingActivity.class);
introIntent.putExtra(PREF_USER_FIRST_TIME, isUserFirstTime);
ProgressDialog dialog = new ProgressDialog(StartUpActivity.this);
dialog.setMessage("Welcome to Mea Vita, please wait till the app loads.");
dialog.setCancelable(false);
dialog.setInverseBackgroundForced(false);
dialog.show();
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
//Here you can send the extras.
startActivity(new Intent(StartUpActivity.this,SlidingActivity.class));
// close this activity
finish();
}
}, 4000);
}
}
This doesn't happen every time,only sometimes. What can be the possible reason for this? how can I stop this?
Any solution? Thank you..
There is a strange issue with newly released Android Studio 2.0 (same issue in 2.1) first time of launching application take longer than usual (e.g. 2, 3 seconds or sometimes screen blinks or goes black) this issue happens only in debug mode and not effect your released APK.
A temporary solution to fix this is disabling instant run:
Settings → Build, Execution, Deployment → Instant Run and uncheck Enable Instant Run
First of all, make as rule to make all data loading in async tasks, you must check activity that you want to start where you load data.
The problem is in your second activity.
oncreate method should be used only to make findviews or start async tasks, don't load any in oncreate or in onstart or in onresume.
Probably you are loading high res images in sliding layout or you loading data in it.
There is another way, load all data in async task on first activity, then with ready data start second activity with already data loaded.
There are a few things that can load slowly.
Android need to read your code from storage and load the classes into ram.
I assume Utils.readSharedSetting(StartUpActivity.this, PREF_USER_FIRST_TIME, "true") reads from preferences. That's a file that you're reading from synchronously.
Actually launching the dialog takes a very small amount of time.
I'd suggest showing your loading inside the activity itself to minimize the work needed to render it.
Also, you can store PREF_USER_FIRST_TIME as a boolean instead of a String.
If I'm bringing Android activities from the stack to the front, how do I refresh them? So to run onCreate again etc.
My code below, in conjunction with setting activities in the Android manifest to android:launchMode="singleTask" allows me to initiate an activity if that activity is not already active within the stack, if it is active within the stack it is then brought to the front.
How do I then, if the activity is brought to the front refresh it so that onCreate is ran again etc.
Intent intent = new Intent(myActivity.this,
myActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
I think FLAG_ACTIVITY_CLEAR_TOP will resolved your problem:
Intent intent = new Intent(myActivity.this, myActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
I don't think there is an explicit way to refresh onCreate, perhaps you may want to add the code you want reloaded into onResume.
This workaround may work if you want to keep your code in onCreate.
finish();
startActivity(getIntent());
If all you want to do is refresh the content, you should override the onResume method, and add in the code to perform the refresh in this method. To do this, use the following code within the activity that you want to perform the refresh, (ie, not the same activity that you are calling startActivity() from):
#Override
protected void onResume(){
super.onResume();
//add your code to refresh the content
}
Tip: If you are using Android Studio, press Alt+Insert (while you have the Java file open), then click Override Methods, find onResume, and it should provide you with a basic template for the method.
The diagram I added shows the order that the methods are run (this is known as the Activity Lifecycle). onCreate() is run whenever an Activity is first created, followed by onStart(), followed by onResume(). As you can see, when a user returns to an Activity, onCreate() is not run again. Instead, onResume() is the first method that is called. Therefore, by putting your code into the onResume() method, it will be run when the user returns to the activity. (Unlike onCreate(), which will not be run again).
Extra info: Since you will be initially setting the data in onCreate() and then refreshing it within onResume(), you might want to consider moving all of your code used to initially set the data to onResume() as well. This will prevent redundancy.
Edit: Based on your following comment, I can give the following solution:
I'm wanting to properly refresh the page, e.g. if there is a variable count initialised at 0. And though running the activity it's has became equal to 300. When the activity is called (intent) then refreshed, count will once again be equal to it's initial value. Do you know how to do this?
Without your current activity's code, there is not much to work with, but here is some pseudo-code as to how I would accomplish your problem:
public class MyActivity extends Activity {
TextView numberTextView;
int numberToDisplay;
#Override
protected void onCreate(Bundle bundle){
super.onCreate(bundle);
setContentView(myContent);
numberTextView = (TextView) findViewById(R.id.numberTextView);
numberTextView.setText(numberToDisplay+"")//converts the integer to a string
}
#Override
protected void onResume(){
super.onResume();
numberToDisplay = 0;
numberTextView.setText(numberToDisplay+"");
}
}
I've a simple widget which consists of an image and a button, the button should launch an activity. I'm trying to write a Robolectric test to test that the activity is launched when the button is clicked
I've two problems, firstly I'm getting an NPE when attempting to click the button:
java.lang.NullPointerException: can't get a shadow for null
at org.robolectric.bytecode.ShadowWrangler.shadowOf(ShadowWrangler.java:415)
at org.robolectric.Robolectric.shadowOf_(Robolectric.java:1020)
at org.robolectric.Robolectric.shadowOf(Robolectric.java:671)
at org.robolectric.shadows.ShadowIntent.fillIn(ShadowIntent.java:454)
at android.content.Intent.fillIn(Intent.java)
at org.robolectric.shadows.ShadowPendingIntent.send(ShadowPendingIntent.java:48)
at android.app.PendingIntent.send(PendingIntent.java)
at org.robolectric.shadows.ShadowRemoteViews$2$1.onClick(ShadowRemoteViews.java:61)
at android.view.View.performClick(View.java:4084)
Also I'm not sure how to get a reference to the activity launched via the button click.
Code for test:
#Test
public void buttonShouldLaunchActivity() throws Exception {
int widgetId = shadowAppWidgetManager.createWidget(HelloWidgetProvider.class, R.layout.hellowidget_layout);
View helloWidgetView = shadowAppWidgetManager.getViewFor(widgetId);
Button quickButton = (Button) helloWidgetView.findViewById(R.id.quick_add_button);
quickButton.performClick();
// Not sure how to get a handle of the activity started from a widget, this is what I have for an activity launched from another activity.
Intent intent = Robolectric.shadowOf(activity).peekNextStartedActivity();
assertEquals(QuickAddActivity.class.getCanonicalName(), intent.getComponent().getClassName());
}
Any thoughts would be apreciated, the actual widget is working (the activity is launched) but I'd just like to have a test for it.
peekNextStartedActivity() is actually a method on the ShadowApplication. The method on ShadowContextWrapper (which is what the ShadowActivity implicitly uses) is actually just a wrapper that calls the one on the shadow application.
So you should be able to do something like this, to get what you need:
Robolectric.getShadowApplication().peekNextStartedActivity()
Normally In an Fragment, implicit intent can be used to start a brower component,if there are several brower component, the Android system will show an selection dialog ,the dialog just overlay the Fragment which send the intent.
but in my situation when selection dialog is shown ,the Fragment go to the background(disappeared) ,and the home screen can be seen.
does anyone encounter this problem ,how to make the Fragment not go to the backgoround.
in onPause method it just look like below in my fragment source
#Override
public void onPause() {
mLog.printMethodLifeCycle("onPause");
super.onPause();
}
and in activity I did not overide the onPause method
AndroidManifest.xml is too long ,can you tell me which part should I post or notice