In android i need to check files exist every time when the app is launched,i need to know the method in android which will be called when the app is launched,on create is called once when app is deployed first,but i need check each time when app is relaunched again?
Oncreate Method of your first activity will call everytime when application launch.so, put below code for checking file existance.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
File f = new File(filePathString);
if(f.exists())
{
/* do something */
}
else
{
/* do something */
}
Related
I have a problem, i want to send a parameter (int) from an app of build in android studio and receive it in an app build it in unity.
The apps work like this, you open the android app, press a botom and open the second app, i send and intent when i open the unity app but idkhow to recieve the param.
I know that are some questions about this but didn't work or maybe idk how to implement it to my code.
This is how i send the param
Java app
Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.bodyar");
if(user!=null){
launchIntent.putExtra(Constant.TYPE,user.getLearningStyleId());
}else{
launchIntent.putExtra(Constant.TYPE,Constant.VISUAL);
}
startActivity(launchIntent);
but i dont know how to receive the value. I tried the stuff of UnityPlayer.UnitySendMessage(), but u just get an error from that
And the funciton of the Script that it´s attached to GameObjectUI is this
public void TipoTest(string token){
prueba.text = token;
}
And when i export the unity project to android, in the method OnCreate I recieve the params like this
public class UnityPlayerActivity extends Activity {
protected UnityPlayer mUnityPlayer; // don't change the name of this variable; referenced from native code
// Setup activity layout
#Override protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
mUnityPlayer = new UnityPlayer(this);
setContentView(mUnityPlayer);
mUnityPlayer.requestFocus();
}
#Override protected void onNewIntent(Intent intent) {
// To support deep linking, we need to make sure that the client can get access to
// the last sent intent. The clients access this through a JNI api that allows them
// to get the intent set on launch. To update that after launch we have to manually
// replace the intent with the one caught here.
setIntent(intent);
}
}
And It works but idk how to pass the variable to unity script
I have a scenario where I have created library project in which I am loading the layout as well defining the method which I want to call from the Application project.
public class LibraryActivity {
public LibraryActivity() {
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.barchart_layout);
bindViews();
}
public void bindViews() {
bChart = (BarChart)findViewById(R.id.barchart);
}
public void setData(int count, float range) {
//definition of the method
// I have to user bChart view here
System.out.println("part 1 "+bChart); <---- this is null
}
Now I have successfully created the AAR project and I want to use this setData from the other project.
So when I run this From the other project By using below code
Intent in = new Intent(MainActivity.this,LibraryActivity.class);
startActivity(in);
LibraryActivity barChartCallBack = new LibraryActivity();
LibraryActivity.setData(15,25);
I got the null pointer on the bChart because my method setData got call first before oncreate so findviewbyid gives me null for the bChart.
You are setting the data on a completely different LibraryActivity object. First you tell the system with the intent to start one, then you start another one that does not get displayed, but you use it to call setData.
Read up on the android developer site:
https://developer.android.com/training/basics/firstapp/starting-activity.html
Basically you need to pass the data via extras in with the intent.
I developed a simple game and I want to give an external URL to my web page. The problem is that, I'm using setContentView in the onCreate method of my main activity. My game content is dynamic and I cannot use intents instead of setContentView. However, as you know, onCreate is called once and the only way that I found on web to call an URL is "using intents". It is not working because setContentView is the only thing running. Here is my codes:
GameActivity.java
private static GameContent gameContent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
gameContent = new GameContent(this);
setContentView(gameContent);
} // on.c.ends
My GameContent class is extends SurfaceView and Runnable. I call all draw(), pause() and resume() methods here. I have a class BasicButton and I added it to GameContent and draw it. It works. Here is the part that I want to use:
BasicButton.java
#Override
public void update() {
if (xx > (rate.getX()) && xx < (rate.getX() + rate.getPlayButton('x')))
if (yy > (rate.getY()) && yy < (rate.getY() + rate.getPlayButton('y'))) {
UserInput.setXY(0, 0);
if (UserInput.getAction() == true) {
// I want to add a code here to open a web page
} // fi
} // fi
} // update ends
My solution was to create a boolean in GameActivity and change it from BasicButton class and using an intent and startActivity, to open a web page. However it is not working.
Do you have any suggestion?
You can't. setContentView only works with pre-compiled XML files in your APK. You can't even write a new one on the fly and call setContentView on it. I don't think you really understand what setContentView does.
I have an Android app, composed of Fragments, that I have saving state correctly. The problem is it works a bit too well ;-). I'll enter some input into a couple of EditText elements that I then save via SharedPrefs in the "onSaveInstanceState()" method, and then hit the "Task Manager" or "Switch App" button on the phone (there's two overlapping rectangles as the icon) and swipe left to close my application. If I then go to the App Drawer and re-run the application, that saved input will still be there. I am clearing the saved instance state in the "onDestroy()" method too, but apparently that is not being called when "closing" the app from that Task Manager (confirmed via logging).
Any suggestions here? Other apps I have do not exhibit this behavior. I'd like for the saved input to be cleared when a user closes the app via Task Manager as well as probably after a set amount of time. Any ideas of what the standard practice for state handling is here?
I tested some apps I have and noticed the default Contacts app actually saves a new contact if you start a new one and switch to another app before explicitly saving. I guess I could do this but I'd rather not.
Below is some relevant code for a particular Fragment; thank you very much in advance for any assistance.
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
Log.v(Tag, "onSaveInstanceState()");
saveInstanceState();
}
#Override
public void onResume() {
super.onResume();
Log.v(Tag, "onResume()");
restoreInstanceState();
}
#Override
public void onDestroy() {
super.onDestroy();
Log.v(Tag, "onDestroy()");
clearInstanceState();
}
private void saveInstanceState() {
Log.v(Tag, "saveInstanceState()");
// get entered data
String name = mTxtName.getText().toString();
String notes = mTxtNotes.getText().toString();
// save data in Shared Prefs
PreferenceManager.getDefaultSharedPreferences(mContext)
.edit()
.putInt(KeyAmmunitionId, mAmmunitionId)
.putString(KeyName, name)
.putString(KeyNotes, notes)
.putString(StringUtils.CurrentFragmentKey, Tag)
.commit();
}
private void restoreInstanceState() {
Log.v(Tag, "restoreInstanceState()");
mTxtName = (EditText)getActivity().findViewById(R.id.frag_manage_ammunition_txtName);
mTxtNotes = (EditText)getActivity().findViewById(R.id.frag_manage_ammunition_txtNotes);
if (PreferenceManager.getDefaultSharedPreferences(mContext).contains(KeyName)) {
String ammunitionName = PreferenceManager.getDefaultSharedPreferences(mContext).getString(KeyName, StringUtils.EMPTY_STRING);
mTxtName.setText(ammunitionName);
}
if (PreferenceManager.getDefaultSharedPreferences(mContext).contains(KeyNotes)) {
String ammunitionNotes = PreferenceManager.getDefaultSharedPreferences(mContext).getString(KeyNotes, StringUtils.EMPTY_STRING);
mTxtNotes.setText(ammunitionNotes);
}
}
private void clearInstanceState() {
Log.v(Tag, "clearInstanceState()");
PreferenceManager.getDefaultSharedPreferences(mContext)
.edit()
.remove(KeyAmmunitionId)
.remove(KeyName)
.remove(KeyNotes)
.commit();
}
instead of using SharedPrefs, I've found that an internal headless fragment that retains its state is much easier to implement/cleaner.
Inside your fragment class you have this class....
/**
* "Headless" Fragment that retains state information between
* configuration changes.
*/
public class RetainedFragment extends Fragment {
/**
* internal storage to be kept put here
*/
// assuming the only 'view' in the parent fragment is this editText with some string value.
String editTextValue;
public RetainedFragment(){
editTextValue = ""; //init values on first time in.
}
/**
* Hook method called when a new instance of Fragment is
* created.
*
* #param savedInstanceState
* object that contains saved state information.
*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Ensure the data survives runtime configuration changes.
setRetainInstance(true);
}
// have getter/setter methods
Then in your outer fagment just 'create' the UI based off the values stored in the inner headless fragment.
OR...
You can use the RetainedFragmentManager that we figured out. Its a bit wonky and can be a bit confusing to figure out at first, but its a headless fragment that allows you to store java objects in hashmap-like manner, and they will persist across configuration changes, but won't exist if your app is fully closed, etc.
https://github.com/douglascraigschmidt/POSA-15/blob/master/ex/ImageDownloads/src/vandy/mooc/common/RetainedFragmentManager.java
New to Android so bear with me.
I have a fragment (DirectionsFragment) that calls an Activity to get some data from Yelp (YelpActivity).
When I run the YelpActivity itself, without calling it from DirectionsFragment, it runs succesfully (displays the received information, prints out appropriate log messages, etc.).
However, when I create YelpActivity from the fragment, nothing happens - no log messages, no retrieved information.
Here's how I create the YelpActivity
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
YelpActivity ya = new YelpActivity();
}
Thanks
Activity is started by startActivtiy(intent). But you have
YelpActivity ya = new YelpActivity();
Should not create an instance of Activity class.
Use
startActivity(new Intent(getActivtiy(),YealpActivtiy.class);
Make sure you have declared the activity in manifest file