Unable to remove activity from stack, - java

I am starting an activity from service. ,
In this i set two flags one is
Intent.FLAG_ACTIVITY_NEW_TASK
because i am running activity from service.its new
Intent.FLAG_ACTIVITY_NO_HISTORY is set to new activity is not kept in the history stack. As soon as the user navigates away from it, the activity is finished.
Here is the code which i call from service to open activity
public static void open(Context context){
Intent intent=new Intent(context,PopupActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_NO_HISTORY);
context.startActivity(intent);
}
and i have cross button in my activity to destroy activity , when i press cross button activity should remove from stack,but it remain in stack and when press home button to view it, activity recreated in stack.for example "oncreate" is again called
here is my code.
#OnClick(R.id.flClose) void closeWindow(){
finish();
}
Here is my manifest activity code.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mypackagename">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application
android:name=".app.MyApplication"
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.NoActionBar">
<activity android:name=".SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".DashboardActivity"
android:screenOrientation="portrait" />
<activity
android:name=".SearchUserActivity"
android:screenOrientation="portrait" />
<activity
android:name=".TermsPolicyActivity"
android:screenOrientation="portrait" />
<activity
android:name=".UserDetailActivity"
android:screenOrientation="portrait" />
<activity
android:name=".DocumentViewerActivity"
android:screenOrientation="portrait" />
<activity
android:noHistory="true"
android:name=".PopupEndActivity"
android:configChanges="orientation|keyboardHidden"
android:theme="#style/Theme.AppCompat.Transparent.NoActionBar" />
<activity
android:name=".BlockActivity"
android:theme="#style/Theme.AppCompat.Transparent.NoActionBar" />
<activity
android:name=".BlockSettingsActivity"
android:theme="#style/AppTheme.NoActionBar"
android:configChanges="orientation|keyboardHidden"
/>
<service
android:name=".service.SyncDataService"
android:enabled="true"
android:exported="false" />
</application>
</manifest>

Try using finishAffinity() instead of finish()

Add the following flags instead of setflags to open activity
intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
And on close button call the function below
private void endTask() {
if (Build.VERSION.SDK_INT >= 21) {
finishAndRemoveTask();
} else {
finish();
}
}

You can get this behaviour by setting the android:noHistory attribute to "true" in the relevant entries in your AndroidManifest.xml file.
For example:
<activity
android:name=".YourActivity"
android:noHistory="true" />

I solved My problem by Adding flag Intent.FLAG_ACTIVITY_NEW_DOCUMENT This flag is used to open a document into a new task rooted at the activity launched
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_NEW_DOCUMENT);
In manifest i have noHistory
android:noHistory="true"

Related

startActivity crashes if I do not open the activity from another path

So I have 2 ways an activity can be opened. One is from the activity flow of:
Main > Tracks > Day > Topic > TrackSelect > TrackInfo
and the other is:
Main > MySchedule > TrackInfo
If I try to get TrackInfo to open up via the second path, it crashes the app.
However, If I go from the first path, then all the way back to the main, then through the second path, it works perfectly. Is there something weird going on?
AndroidManifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="fayko.conference_app">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="Conference-App"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".mainSelection"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".topicScreen"/>
<activity
android:name=".myScheduleScreen" />
<activity android:name=".trackSelection" />
<activity android:name=".mainScreen" />
<activity android:name=".daySelection" />
<activity android:name=".trackInfoScreen" />
<activity android:name=".mapChoose" />
<activity android:name=".sponsorScreen" />
<activity android:name=".committeeScreen" />
<activity android:name=".welcomeScreen"></activity>
</application>
</manifest>
Code from TrackSelect > Track Info:
Intent intent = new Intent(trackSelection.this,trackInfoScreen.class);
startActivity(intent);
Code from MySchedule > TrackInfo:
Intent intent = new Intent(myScheduleScreen.this,trackInfoScreen.class);
startActivity(intent);
I appreciate any help you guys can give me.
It turns out that the issue I was having was due to some internal code. Even thought he stacktrace was saying things about socket lost for the debugger.

Android - App crashing with "java.lang.ClassCastException"

I am using a tutorial provided by Google to implement Analytics in my app but something I possibly did wrong that causes the app to crash with java.lang.ClassCastException
This is what Google provided:
// Obtain the shared Tracker instance.
AnalyticsApplication application = (AnalyticsApplication) getApplication();
mTracker = application.getDefaultTracker();
This is the changes I made because I am using a Fragment
// This is where I get the error
AnalyticsApplication application = (AnalyticsApplication) getContext().getApplicationContext();
mTracker = application.getDefaultTracker();
UPDATE : The error happens at this line:
AnalyticsApplication application = (AnalyticsApplication) getContext().getApplicationContext();
This is my LogCat
FATAL EXCEPTION: main
Process: com.incorp.labs.appname, PID: 14095
java.lang.ClassCastException: android.app.Application cannot be cast to com.incorp.labs.appname.Helper.AnalyticsTracker
at com.incorp.labs.appname.OneFragment.onCreateView(OneFragment.java:126)
UPDATE 2 : This is the Manifest file
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.incorp.labs.appname">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="com.android.vending.BILLING" />
<application
android:allowBackup="true"
android:icon="#mipmap/newlogops"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/MyMaterialTheme">
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<activity
android:name=".Splash"
android:screenOrientation="portrait" />
<activity
android:name=".MainActivity"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustPan" />
<activity
android:name=".OneFragment"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustPan" />
<activity
android:name=".TwoFragment"
android:screenOrientation="portrait" />
<activity
android:name=".Feedback"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustPan" />
<activity
android:name=".FourFragment"
android:screenOrientation="portrait" />
<activity
android:name=".SplashTimer"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:theme="#android:style/Theme.Translucent" />
<service android:name=".FirebaseMessagingService">
<intent-filter>        
<action android:name="com.google.firebase.MESSAGING_EVENT" />
 
</intent-filter>
</service>
<activity android:name=".AboutActivity"></activity>
</application>
Just change this :-
AnalyticsTracker application = (AnalyticsTracker) getContext().getApplicationContext();
to this :-
AnalyticsApplication application = (AnalyticsApplication) getContext().getApplicationContext();
It's one of those obvious errors that happen with the best of us!
In addition to implementing the extends Application class, you need to tell Android system which class to use as an Application class. To do so, set the name of your application class in manifest:
<application
android:name="your.package.AnalyticsApplication"
I think you're trying to use a Context object (getApplicationContext()) as an application object.
Try this: EDIT
AnalyticsApplication application = (AnalyticsApplication) getActivity().getApplication();
From your fragment you can call the activity that contains the fragment. When you get the activity, you can use the getApplication() function as in the example.
I hope it helps.
EDIT 2
As seen in this answer: https://stackoverflow.com/a/35905601/4336354 from CommonsWare user.
Your AndroidManifest.xml file does not contain your custom Application
subclass, so Android is using the default one
(android.app.Application).
Add an android:name="auc.games2.Analytics.AnalyticsApplication"
attribute to your element.

How to change first activity in Android Studio?

I want to change my first activity, first I have activity named MainActivity. And I add new activity named ActivityFirst. Surely I have to change AndroidManifest.xml. Previously I have androidManifest like this:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="edmt.dev.androidsimsimi">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
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>
And after I add my new activity, also I change my androidManifest file.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="edmt.dev.androidsimsimi">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".ActivityFirst">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
I'm confused where I put the MainActivity after I add ActivityFirst. This my ActivityFirst.java:
public class ActivityFirst extends AppCompatActivity {
private Button mulai;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
mulai = (Button) findViewById(R.id.mulai);
mulai.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(ActivityFirst.this, MainActivity.class);
startActivity(intent);
}
});
}
}
You need <activity> tags for each Activity in your app's manifest file as mentioned here in the docs:
All activities must be represented by elements in the
manifest file. Any that are not declared there will not be seen by the
system and will never be run.
So in your case, it'd be like this:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="edmt.dev.androidsimsimi">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".ActivityFirst">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity" />
</application>
</manifest>
In Android, The first Activity to show when a app is launched is tagged LAUNCHER in your manifest file.
All you need do to change the Launcher Activity is to add an intent-filter tag inside the activity tag.
So if your activity tag was self closing, you change it and add intent filter tag inside the tag.
Here is an illustration.
<activity android:name=".MainActivity" />
<activity android:name=".ActivityFirst">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
The above code in your Manifest will make ActivityFirst the launcher activity,
while the below code will make MainActivity the launcher activity.
<activity android:name=".ActivityFirst" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
I hope this helps.

DisplayMessageActivity back button keeps crashing my android application

The default back button that appears next to the icon in the action bar crashes when i try to return to MainActivity. MainActivity calls DisplayMessageActivity from there I click the default back button and the app crashes.
Logs show message:
E/AndroidRuntime(24509): java.lang.IllegalArgumentException: Activity DisplayMessageActivity does not have a parent activity name specified. (Did you forget to add the android.support.PARENT_ACTIVITY <meta-data> element in your manifest?)
Here is my manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.assemblyx.playzone"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="net.assemblyx.playzone.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="net.assemblyx.playzone.DisplayMessageActivity"
android:label="#string/title_activity_display_message"
android:parentActivityName="net.assemblyx.playZone.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="net.assemblyx.playZone.MainActivity" />
</activity>
</application>
package name had playZone and should have been playzone

Android: How to start an Activity via button?

Noob Android developer here I'm more graphics than code but thought I'd start doing more coding. Anyway, I have some buttons on my main activity page and I want it so when the button is clicked it opens another class/activity. I've tried all the methods I've looked up and something is still not working, when I click the button in the emulator it just doesn't do anything, does't forcestop or anything just nothing, someone point me in the right direction please.
Code from the main page where the button lives:
public class StartingPoint extends Activity {
protected void onCreate(Bundle aim) {
super.onCreate(aim);
setContentView(R.layout.main);
final Button bSL = (Button) findViewById(R.id.bSongList);
bSL.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent SongList = new Intent(StartingPoint.this, SongList.class);
StartingPoint.this.startActivity(SongList);
}
});
}
}
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="myname.appname"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#android:style/Theme.Black.NoTitleBar" >
<activity
android:name=".Splash"
android:label="#string/app_name"
android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen" >
<intent-filter>
<action android:name="myname.appname.SPLASH" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".StartingPoint"
android:label="#string/app_name" >
<intent-filter>
<action android:name="myname.appname.STARTINGPOINT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".SongList"
android:label="#string/app_name" >
<intent-filter>
<action android:name="myname.appname.SONGLIST" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
-L
Last 3 lines of logcat just after clicking the button in question.
01-02 21:59:50.473: I/ActivityManager(75): Starting: Intent { cmp=myname.appname/.SongList } from pid 681
01-02 21:59:52.953: I/ActivityManager(75): Displayed myname.appname/.SongList: +2s351ms
01-02 21:59:58.565: D/dalvikvm(348): GC_EXPLICIT freed 8K, 55% free 2591K/5703K, external 1625K/2137K, paused 520ms
Try this:
Intent songList = new Intent(StartingPoint.this, SongList.class);
startActivity(songList);
Are you getting any errors? You need to add every Activity you create in the manifest. If you have two activities and only the Main in the manifest, that could be you problem.
On second thought, I believe your manifest is wrong. Check this. With the Main (Your starting point) and the Menu which is the second one:
<activity android:name="com.activities.Main">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.activities.Menu"></activity>
Try this manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="myname.appname"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#android:style/Theme.Black.NoTitleBar" >
<activity
android:name=".Splash"
android:label="#string/app_name"
android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".StartingPoint"
android:label="#string/app_name" >
</activity>
<activity
android:name=".SongList"
android:label="#string/app_name" >
</activity>
</application>
</manifest>
You don't need to add the filter on each activity. I understood that you have only one entry Activity and that is the splash screen. However, you might need to change the manifest to change the ".Splash" and every other Activity to the full path including the package.
// whats your package name for your SongList
as your log cats shows
01-02 21:59:50.473: I/ActivityManager(75): Starting: Intent { cmp=myname.appname/.SongList } from pid 681
01-02 21:59:52.953: I/ActivityManager(75): Displayed myname.appname/.SongList: +2s351ms
//use full package name in your activity in manifest.xml
<activity
android:name="myname.appname.SongList"
android:label="#string/app_name" >
</activity>
Your activity does not start because it has a NullPointerException in it. (Line 10). When you do a findViewById on something that is not in your layout, most likely.
When facing this kind of problems, reading the red lines is usually helpful. It basically says: "Hey! you have a null object in SongList line 10 when you try to start it!"
Edit
ImageView ivlogo = (ImageView) findViewById(R.id.ivsonglogo);
ExpandableListView elv1 = (ExpandableListView) findViewById(R.id.elv1);
This can never work. This is done during the object initialization, much before onCreate is called, therefore much before you have called setContentView.
You must initialize your widgets after having set the view.
Intent SongList = new Intent(StartingPoint.this, SongList.class);
startActivity(SongList);
and in Manifest type this
<application .....>
<activity android:name=".CustomSurfaceView"></activity>
</application>
For the code try this:
Intent songListIntent = new Intent(this, SongList.class);
StartingPoint.this.startActivity(songListIntent);
And for manifest:
<activity android:name=".SongList"></activity>

Categories