I'm struggling with my application lately.
I have an activity 'MyActivity' with a textview holding the current date
The use case is simple:
- user clicks a button to start camera intent from MyActivity
- user takes picture
- MyActivity is resumed and the textview still holds the current date
This works fine on most devices. However, on devices with low memory, the activity is destroyed during camera intent and when I get back to MyActivity,
the textview no longer holds the date.
Using debugger and breakpoints, I could see the following:
05-16 12:41:35.364 3614-3614/com.example.test D/TAG: onDestroy: MyActivity#10140ca0
05-16 12:41:35.414 3614-3614/com.example.test D/TAG: onCreate: MyActivity#2e507264
05-16 12:41:35.804 3614-3614/com.example.test D/TAG: setting textview date : "16 05 2018"
05-16 12:41:37.206 3614-3614/com.example.test D/TAG: onDestroy: MyActivity#2e507264
05-16 12:41:37.236 3614-3614/com.example.test D/TAG: onCreate: MyActivity#253f67dc
05-16 12:41:37.706 3614-3614/com.example.test D/TAG: setting textview date : "16 05 2018"
As you can see the textview is being set just as expected, but the screen doesn't show it.
If anyone can guess the solution to this annoying I would be thankful.
Edit
#Override
public void onCreate(Bundle savedInstanceState) {
Log.d("TAG", "onCreate: " + this);
// inflate the layout
loadContent(R.layout.activity_invoice_more_options);
dateView = findViewById(R.id.date_view);
buttonView = findViewById(R.id.button_view);
dateView.setText(Tools.getFormattedDate(Calendar.getInstance().getTime(), "dd MM yyyy"));
// set button click listener
buttonView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent chooseImageIntent = getPickImageIntent(getApplicationContext(), getString(R.string.choose_action));
startActivityForResult(chooseImageIntent, FILE_PICKER_RESULT);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (data == null) return;
switch (requestCode) {
case FILE_PICKER_RESULT:
if (resultCode != RESULT_OK) return;
// retrieve file content and name
final String fileName = Tools.getFileName(this, data);
String fileContent = Tools.getFileContent(this, data);
break;
}
}
Related
First of all I used the idea from http://www.tutorialsface.com/2016/05/implementing-remove-ads-in-app-purchases-in-android-tutorial-example/ having a separate class for dealing InApp billing.
My class from which I start the billing had an already onActivityResult method which I changed it to:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (mHelper == null) return;
if (!mHelper.handleActivityResult(requestCode, resultCode, data))
{
if(requestCode==3)
{
// Construct the data source
ArrayList<Service> arrayOfServices = new ArrayList<Service>();
arrayOfServices = db.getAllServices();
// Create the adapter to convert the array to views
CatalogueAdapter adapter = new CatalogueAdapter(this, arrayOfServices);
// Attach the adapter to a ListView
myServices.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
if (requestCode == 1111 && resultCode == RESULT_OK)
{
String emailAddress = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
Calendar c = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd_HH:mm");
String formattedDate = df.format(c.getTime());
possibleEmail=md5(emailAddress + formattedDate);
startBilling.purchaseRemoveAds(possibleEmail);
}
}else
{
//Log.d(TAG, "onActivityResult handled by IABUtil.");
}
}
First of all,I want to get the main google account and encrypted together with the current date and then start the Billing process. But in front of the dialog from which I get the email I have the dialog of InApp Billing.
So I thought to place the method of StartBilling inside the Activity Result.
But even if I get the email dialog first I get a null on mHelper so nothing happens next. How To fix this?
Shall I do the following?:
#Override
protected void onResume() {
super.onResume();
if(possibleEmail!=null)
{
startBilling.purchaseRemoveAds(possibleEmail);
}
}
Null object reference came from not initializing the mHelper onCreate method of my second activity so I did
mHelper = startBilling.mHelper;
I want to use android's speech to text. following is my MainActivity.java
public class MainActivity extends AppCompatActivity {
private TextView txtSpeechInput;
private ImageButton btnSpeak;
private final int REQ_CODE_SPEECH_INPUT = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtSpeechInput = (TextView) findViewById(R.id.txtSpeechInput);
btnSpeak = (ImageButton) findViewById(R.id.btnSpeak);
// hide the action bar
//getActionBar().hide();
btnSpeak.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i("OnClickListener CALLED:", "");
promptSpeechInput();
}
});
}
private void promptSpeechInput() {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
getString(R.string.speech_prompt));
try {
startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
} catch (ActivityNotFoundException a) {
Toast.makeText(getApplicationContext(),
getString(R.string.speech_not_supported),
Toast.LENGTH_SHORT).show();
}
Log.i("promptSpeechInput :", "Prompt speech CALLED");
}
/**
* Receiving speech input
* */
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.i("OnActivity CALLED1:","one");
switch (requestCode) {
case REQ_CODE_SPEECH_INPUT: {
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> result = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
txtSpeechInput.setText(result.get(0));
Log.i("OnActivity CALLED2:", result.get(0));
}
break;
}
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Here is my Log:
02-26 16:23:05.047 13636-13636/com.example.vishal.speechtotext I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy#39ca1da6 time:158833044
02-26 16:23:09.637 13636-13636/com.example.vishal.speechtotext I/Timeline: Timeline: Activity_launch_request time:158837632
02-26 16:23:09.707 13636-13636/com.example.vishal.speechtotext I/promptSpeechInput :: Prompt speech CALLED
02-26 16:23:42.797 13636-13636/com.example.vishal.speechtotext I/OnActivity CALLED1:: one
02-26 16:23:42.837 13636-13636/com.example.vishal.speechtotext I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy#39ca1da6 time:158870837
code just runs, doesn't show any error. i am unable to find what the
error is.
I am testing on Lollipop
Do i need net connection?
any help will be appreciated.
Yes you'll be needing Internet for this. Paste Following permissions inside your manifest file:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Right now all the devices are not supporting offline speech input. To enable offline speech input for supported devices follow below steps:
On your device go to Settings -> Language and Input. Click on icon on Google voice input.
Under ALL tab select the language you want to download.
3. Once the language package downloaded, you can see it under INSTALLED tab.
I have downloaded speech input packages on my Nexus 5 and offline speech is working fine.
For further reference checkout the below link:
https://web.archive.org/web/20160218040751/http://www.androidhive.info/2014/07/android-speech-to-text-tutorial/
I am using a Fragment to start a new Activity using startActivityForResult(), I am getting the result (Bundle) in onActivityResult() method.Since onActivityResult() called before onResume().I want to make sure, I keep/save the Bundle properly so that when Fragment's onResume() gets called, I get the kept/saved result to perform further action.
What are the different ways to achieve this. I tried using getArguments()/setArguments(), but that seems to be not the right way to achieve this.
Try this
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == MY_REQUEST) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
mResultBundle = data.getExtras(); //mResultBundle is in fragment
//scope
}
}
}
#Override
protected void onResume(){
super.onResume();
if(mResultBundle != null){
// process saved bundle from activity result here
// don't forget to set it back to null once you are done
mResultBundle = null;
}
}
I've extended Deitel's "Flag Quiz" (Android for Programmers...App Driven...) to (among other things) allow user to display a small flag for comparison with the given flag. The user taps SHOW FLAG and then gets a list of countries, taps one, and it's returned and shown (as ... shown below).
The only problem I'm having is that after tapping the desired country, the Settings preferences screen shows (see below). I guess this is because SHOW FLAG is part of it, but I want the next thing the user sees after selecting country from list is to be the screen above. There is no need to see the screen below, so there's definitely no reason to have to press the back button--how to avoid?
FYI, here is a trace and relevant code snippets (I can think of no reason to show any xml):
MainActivity.java:
07-06 17:25:48.305 32269-32269/com.dslomer64.flagquiz W/opOptionsItemSelected? item is <Show flag> -----------------------
07-06 17:25:48.350 32269-32269/com.dslomer64.flagquiz W/Here's where? to get flag
07-06 17:25:48.688 32269-32269/com.dslomer64.flagquiz W/onOptionsItemSelected? Show flag showFlag is true
`FlagActivity.java`:
07-06 17:25:48.768 32269-32269/com.dslomer64.flagquiz W/FlagAct? onCreate
07-06 17:25:51.707 32269-32269/com.dslomer64.flagquiz W/returnToA from FlagAct? Anguilla!!!
`SettingsActivity`:
07-06 17:25:51.751 32269-32269/com.dslomer64.flagquiz W/onCreate? SettingsActivity
(not me)
07-06 17:25:51.783 32269-32269/com.dslomer64.flagquiz W/Resources? Converting to string: TypedValue{t=0x10/d=0x3 a=-1}
07-06 17:25:51.786 32269-32269/com.dslomer64.flagquiz W/Resources? Converting to string: TypedValue{t=0x10/d=0x7d0 a=-1}
`MainActivity.java`:
07-06 17:25:55.554 32269-32269/com.dslomer64.flagquiz W/entering? onActivityResult in MainActivity
07-06 17:25:55.569 32269-32269/com.dslomer64.flagquiz W/onActResult? in Main Act show one flag Anguilla
`QuizFragment.java`:
07-06 17:25:55.591 32269-32269/com.dslomer64.flagquiz W/in showAFlag? country: Anguilla
MainActivity.java, which is where Preferences are kept and options are selected
and result of calling FlagActivity (onActivityResult) is processed:
#Override public boolean onOptionsItemSelected(MenuItem item){
Log.w("opOptionsItemSelected", "item is <" + item.getTitle() + "> -----------------------");
Intent preferencesIntent = new Intent(this, SettingsActivity.class);
startActivity(preferencesIntent);
Button getData = (Button)findViewById(R.id.button2);
Intent intent = new Intent(
MainActivity.this,
FlagActivity.class
);
startActivityForResult(intent, 0);
Log.w("onOptionsItemSelected", "" + item.toString() + " showFlag is " + showFlag);
return super.onOptionsItemSelected(item); // to SettingsActivity.onCreate
}
#Override protected void onActivityResult(int requestCode, int resultCode, Intent data ) {
Log.w("entering","onActivityResult in MainActivity");
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 0 && resultCode == Activity.RESULT_OK){
String enteredData = data.getStringExtra("Data");
Toast.makeText(getApplicationContext(), "Returned " + enteredData, Toast.LENGTH_SHORT).show();;
country = enteredData;
Log.w("onActResult "," in Main Act show one flag " + country);
quizFragment.showAFlag(country);
// ABOVE IS WHERE FLAG IS SHOWN ^^^
}
}
FlagActivity.java, which is where the listview is processed:
#Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_flag);
Log.w("FlagAct", " onCreate");
flagAdapter = new ArrayAdapter<String>(this, R.layout.list_item, flagArray);
setListAdapter(flagAdapter);
getListView().setOnItemClickListener(itemClickListener);
}
AdapterView.OnItemClickListener itemClickListener = new AdapterView.OnItemClickListener() {
#Override public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
String country = ((TextView) view).getText().toString();
Intent result = new Intent();
result.putExtra("Data", country);
setResult(Activity.RESULT_OK, result);
Toast.makeText(getApplicationContext(), country + "!!!", Toast.LENGTH_SHORT).show();
Log.w("returnToA from FlagAct", country + "!!!");
finish();
}
}; // end itemClickListener
.
SettingsActivity.java:
public class SettingsActivity extends Activity // hosts SettingsFragment in portrait mode
{
#Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.w("onCreate", "SettingsActivity");
setContentView(R.layout.activity_settings);
}
Set an Preference.OnPreferenceChangeListener using the setOnPreferenceChangeListener() method.
Then, when the country is changed, call the finish() method to exit your activity
I fixed my problem by modifying onOptionsItemSelected, but I did not do so in terms of how I asked the Question.
showFlag = (item.toString().equals("Show flag"));
if(showFlag) // If the Settings buttonpress requested a flag,
{ // start the activity for it
Intent intent = new Intent(
MainActivity.this,
FlagActivity.class
);
startActivityForResult(intent, 0);
} // but don't start a preferences activity.
else // But if the Settings buttonpress was NOT 'Show flag',
{ // start a preferences activity.
Intent preferencesIntent = new Intent(this, SettingsActivity.class);
startActivity(preferencesIntent);
};
I still don't know (and don't need to) how to "dismiss" a Settings screen.
But it would be good to know how to do so, if it's possible.
I'm working on an app that take picture by calling the Camera Intent. In the next Activity I use the URI of the image that I got and display the image. It works fine.
The problem is when I test in Sony Neo V device (ICS), the image get rotated 90 degrees (this is the screenshot). It doesn't happen when I test in HTC Desire device (Gingerbread) (this is the screenshot).
Here is my code:
Activity 1:
private final int CAMERA_REQUESTCODE = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.memberform);
Button photo = (Button) findViewById(R.id.btn_photo);
photo.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUESTCODE);
}
});
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if ( requestCode==CAMERA_REQUESTCODE ) {
if ( resultCode==RESULT_OK ) {
GlobalVar.member.setPhotoUri(data.getData());
} else if ( resultCode==RESULT_CANCELED ) {
} else {
Toast.makeText(this, "Unknown onActivityResult resultCode = " + resultCode, Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(this, "Unknown onActivityResult requestCode = " + requestCode, Toast.LENGTH_SHORT).show();
}
}
Activity 2:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.memberdetail);
ImageView photo = (ImageView) findViewById(R.id.photo);
photo.setImageURI( GlobalVar.member.getPhotoUri() );
}
I've tried to detect if ( ImageView.getWidth()>ImageView.getHeight() ) then rotate90degrees(); but it doesn't work. And I'm hoping there's a general working code (works on any device) that solve this problem because it would be better than making a conditional if.
Any help & explanation would be appreciated. General working code would be greatly appreciated.
Many thanks
There seems to be a bug with other devices as well, not sure if all Samsung devices do it, but quite a few are doing it. I can confirm on my device Samsung infuse.
You may have to query the ContentResolver to get the orientation for each image and see if it needs rotating.