I was trying to make application, it was working but now when I run it in emulator it's not even giving me the welcome screen. Code:
public class TestArabActivity extends Activity {
/** Called when the activity is first created. */
TextView tvTop,tvBottom;
TableLayout tlStart;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome_screen);
//init variables
initVars();
}
private void initVars(){
tvTop = (TextView) findViewById(R.id.tv_start_TopTittle);
tvBottom = (TextView) findViewById(R.id.tv_strat_BottomTittle);
tlStart = (TableLayout) findViewById(R.id.tl_start);
}
}
I used debugger and saw these:
the value of savedInstanceState = null
values of tvTop, tvBottom and tlStart are null also
Why those values are null? I know this is the problem.
Anyone know how to solve it? Thanks.
Use DDMS to debug your application,if it throws any exception, you can see from the stack trace where the problem comes from. if it does not throw any exception. then probably you forgot to declear intent-filter in the menifest:
<activity android:name="TestArabActivity" android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Related
I need to close an activity when a button is clicked. Unfortunately, when button is clicked, the activity does disappear but is still in the background. User can still select it and it comes back to front. What I need is the activity completely gone/destroyed.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
finish();
}
});
}
#Override
protected void onDestroy() {
super.onDestroy();
}
}
I searched on SO on related questions, however, none of them help with closing the activity completely. I already tried adding return, adding another broadcast listener and passing command to call finish outside onCreate. So at this point, the question is - is this really possible or is this how Android works, you can call finish() but it is still in the background and user can re-launch it.
Here is xml file.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.app1.test.myapplication">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
EDIT: Adding android:excludeFromRecents="true" does not solve the issue. Here are steps to recreate this, if anyone thinks it is a duplicate or already solved, please try and post comment/answer, I will accept it.
Open Android Studio
Create empty activity project.
Add a button.
Add code in MainActivity's onCreate for button click listener.
Inside click listener, call finish.
Run the app and click button and see if the activity is still in background or not.
Just give it a try.
In you manifest.
<activity
android:excludeFromRecents="true"
android:name=".Activities.SplashActivity"
android:theme="#style/AppThemeSubActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Now in your java code.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.item_search:
// Intent intent = new Intent(MainActivity.this, SearchActivity.class);
// startActivity(intent);
finish();
System.exit(0);}
}
put the extra line System.exit(0); after calling finish it works for me.
You need to put this in your XML manifest:android:excludeFromRecents="true"
in your Activity TAG.
<activity
...
android:excludeFromRecents="true">
</activity>
You could try this
android:excludeFromRecents="true", Use it in your manifest.
plz try this to go back
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
onBackPressed();
}
});
}
}
and this code to kill app process
moveTaskToBack(true);
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(1);
I've read a few articles here (and other places) that describe how to dynamically choose which activity to show when launching an app. Below is my code:
AndroidManifest.xml
<activity android:name=".StartupActivity"
android:theme="#android:style/Theme.NoDisplay">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
StartupActivity.java
public class StartupActivity extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Intent intent;
if (RandomClass.getSomeStaticBoolean())
{
intent = new Intent(this, ActivityOften.class);
}
else
{
intent = new Intent(this, ActivityRare.class);
}
startActivity(intent);
finish();
}
}
Both ActivityOften and ActivityRare are declared in the manifest (without the launcher category of course) and extend ListActivity and Activity respectively. 99% of the time the 1st activity to get shown is ActivityOften based on RandomClass.getSomeStaticBoolean().
So launching my app from the icon for the 1st time I break inside the StartupActivity.onCreate. The choice is properly made. But then any subsequent attempts to launch the app (from a shortcut or the apps menu) show the ActivityOften again. No further breaks occur inside the StartupActivity class. Despite the fact that I know that RandomClass.getSomeStaticBoolean() has changed value and that ActivityRare should appear, the 1st activity keeps popping up.
Any ideas?
Thanks, Merci, Gracias, Danke, Grazie!
Sean
It is happening because your application activity is loaded from the history stack.
Set android:noHistory=true in the manifest for both ActivityOften and ActivityRare. That should solve your problem.
Just as a suggestion, you could just have one activity instead of three by choosing the content View dynamically. i.e.
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
if (RandomClass.getSomeStaticBoolean())
{
setContentView(R.layout.Often);
// Set up often ....
}
else
{
setContentView(R.layout.Rare);
// Set up rare ....
}
}
This would mean that you would have to write setup code both views in on activity, which can get a bit messy.
in my app I am trying to add new screen. In my activity I've:
public void addItem(View v) {
Intent i = new Intent(SQLiteListActivity.this, add_screen.class);
startActivity(i);
}
In add_screen.java:
public class add_screen extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText("Hello, Android");
setContentView(tv);
}
}
In file add_screen.xml I've layout this screen. In file AndroidManifest.xml I added this next activity, especially:
<activity class=".Add_screen" android:name="ADD_ITEM" android:label="Add item">
</activity>
I am still getting an error message about "Application has been stopped." I am newbie in Android development, I tried to do this by some tutorial, I've everything by it, but I don't know why, I'm still getting the error message above...
Can you help me, please, with this problem? I've no idea, what could be wrong.
Try replacing the activity tag in your manifest with this instead:
<activity
android:name=".add_screen"
android:label="Add item"
>
<intent-filter>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
You shouldn't need the class attribute, I don't even know if that's recognized.
Edit: Try the above instead?
I have an Activity that holds a list. Via the Android onSearchRequested() I implemented a search.
The results are shown as a list with the same adapter in another Activity. Working fine so far.
Also, I want to be able to search from that second Activity showing the new results in the same list.
My AndroidManifest.xml for the two activities:
<activity android:name=".ListActivity" android:label="List">
<meta-data android:name="android.app.default_searchable" android:value=".SearchActivity" />
</activity>
<activity android:name=".SearchActivity" android:label="Results">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable" android:resource="#xml/search" android:value=".SearchActivity" />
</activity>
The SearchActivity's onResume():
#Override
protected void onResume() {
super.onResume();
Intent queryIntent = getIntent();
String value = queryIntent.getStringExtra(SearchManager.QUERY);
setView(value);
}
The setView() method does a foreach loop through all objects adding them to a result-array which is used for a new Adapter that the list shows.
ca = new CustomAdapter(this, R.layout.customadapter, resultArray);
list.setAdapter(pa);
list.invalidate();
When trying to search from the second Activity the search bar appears, I can enter my search value, send it - but the list doesn't change (and even the keyboard stays).
What's missing?
Edit: Tried to make it easier to understand.
Found that question which describes kind of the same problem.
Instead of overriding onResume() I have to override onNewIntent()
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
String value = intent.getStringExtra(SearchManager.QUERY);
setView(value);
}
I am following this tutorial: link text
Preferences.java:
public class Preferences extends PreferenceActivity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
PreferencesTutorial.java:
public class PreferencesTutorial extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button prefBtn = (Button) findViewById(R.id.prefButton);
prefBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent settingsActivity = new Intent(getBaseContext(),
Preferences.class);
startActivity(settingsActivity);
}
});
}
}
Preferences.xml:
When application starts, and i click the prefButton, an error occures: "The application PreferencesTutorial (process PreferencesTutorial.com.examples) has stopped unexpectedly. Please try again"
I haven't found any mistakes in the code.
I would also like to show my filestructure if that helps:
AndroidManifest.xml:
What is wrong with the code?
Even if i add (where the cursor is)
<activity
android:name=".Preferences"
android:label="#string/set_preferences">
</activity>
i still get the error.
Try removing this import, if you have it;
import java.util.prefs.Preferences;
You have to mention this in your androidManifest.xml file
<activity
android:name=".Preferences"
android:label="#string/set_preferences">
<intent-filter>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
You probably do not have Preferences defined in your manifest.
However, as others have indicated, use adb logcat, DDMS, or the DDMS perspective in Eclipse to examine LogCat and see the stack trace associated with your crash.
Is the error raised in the OnClick in PreferencesTutorial Class or onCreate in the preferences Class? Stick a couple of Log.d("Debug","%ID") in various locations and see which one doesn't get called.