This is a simple operation and seems to be working until I've changed my minSdk = 8 to 9 , and targetSdk remains 21 in my manifest.
Problem is that i have an activity A , and i am going to activity B when a button is pressed in activity A . Now on activity B whenever someone press back button i want to clear the activity stack and transfer my activity B to activity C . but instead it finish activity B and go to Activity A , so far i have tried onKeyDown , onBackPressed , nothing seems to work , kindly help.
Activity A (in onClick method):
Intent in = new Intent(A.this,
B.class);
in.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(in);
[UPDATED]Activity B :
#Override
public void onBackPressed() {
Intent in = new Intent(this, C.class);
Bundle bundle = new Bundle();
bundle.putSerializable("Scores", score);
in.putExtras(bundle);
startActivity(in);
}
Activity C:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.C);
ScoreSaver scores = (ScoreSaver) getIntent().getExtras().getSerializable("Scores");
}
Add android:noHistory="true" to both Activity A and B in your manifest file. This way these activities won't be in your back stack.
In Activity B you have to override the behavior of onBackPressed().
It has to look like this:
#Override
public void onBackPressed() {
Intent intent = new Intent(this, ActivityC.class);
startActivity(intent);
}
If you leave super.onBackPressed() in your method, the default behavior will happen, i.e. Activity B will be closed and you jump back to Activity A.
Issue was with Serializable object which was using storing context which will be null in next activity , which causes exception , that crashes my activity. so i removed it everything works fine. Thanks #Chris Fox
Unfortunately I do not have a Git Account, so I post the files' content here. (The app's theme inherits from Theme.AppCompat.Light.DarkActionBar)
Manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="fox.chris.activitytest">
<application android:allowBackup="true"
android:label="#string/app_name"
android:icon="#mipmap/ic_launcher"
android:theme="#style/AppTheme">
<activity
android:name=".ActivityA"
android:noHistory="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ActivityB"
android:noHistory="true">
</activity>
<activity android:name=".ActivityC">
</activity>
</application>
</manifest>
Activity A:
public class ActivityA extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_a);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(ActivityA.this, ActivityB.class);
startActivity(intent);
}
});
}
}
Activity B:
public class ActivityB extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_b);
}
#Override
public void onBackPressed() {
Intent intent = new Intent(this, ActivityC.class);
startActivity(intent);
}
}
Activity C:
public class ActivityC extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_c);
}
}
Related
If I press the share button that appears by long pressing the text, I want to send the text to other activities of my app. I move to another activity, but the text I want is not delivered. What is the problem?
This is the code of the activity that writes the text I want to deliver.
public class MainActivity extends AppCompatActivity {
EditText editText;
Intent sendIntent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.editText);
sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_VIEW);
editText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
sendIntent.putExtra("TEXT", editText.getText().toString());
sendIntent.setType("text/*");
}
});
}
}
This is the intent-filter of my xml code.
<intent-filter>
<action android:name="android.intent.action.SEND" />
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER" />
<data android:mimeType = "text/*"/>
</intent-filter>
This is the part that receives the intent of the activity that I want to receive data from.
Intent receiveIntent = getIntent();
url = receiveIntent.getStringExtra("TEXT");
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(url);
This is the intent-filter part of the xml code of the activity where I want to receive data.
<activity android:name=".MyWebBrowser"
android:label="MyWebBrowser">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/*"/>
</intent-filter>
Here is a simple example for you:
Instead of long press on editText I simply use a button, but nevertheless, the logic stays almost the same:
// MainActivity. From here we send the text from EditText to SecondActivity.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = findViewById(R.id.button);
EditText editText = findViewById(R.id.editText);
Intent intent = new Intent(this, SecondActivity.class);
intent.setAction("MyIntentAction");
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
intent.putExtra("TEXT", editText.getText().toString());
startActivity(intent);
finish();
}
});
}
}
// SecondActivity. Receives and displays text from MainActivity.
public class SecondActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
TextView textView = findViewById(R.id.textView);
Intent intent = getIntent();
if(intent.getAction().equals("MyIntentAction")) {
String str = intent.getStringExtra("TEXT");
textView.setText(str);
}
}
}
// For Long Click detection add the following code to MainActivity:
editText.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
intent.putExtra("TEXT", editText.getText().toString());
startActivity(intent);
finish();
return true;
}
});
I see that you are using two activity has LAUNCHER with the MAIN in your manifest. Please use only one in your manifest.
To pass the data to another activity in your app, you can try to use startActivity and put your data in the intent data:
On action to open the MyWebBrowser:
val intent = Intent(context, MyWebBrowser::class.java)
intent.putExtra("TEXT", yourtext)
startActivity(intent)
Get the Text in your MyWebBrowser:
override fun onCreate(savedInstanceState: Bundle?) {
...
val TEXT = intent.getStringExtra("TEXT")
textView.text = TEXT
}
I have the following Problem: I want to create a Splash Screen, that I can call during my Game Loop (it is a "Next-Level" splash screen).
When I call the Splash Screen, it works with animation and everything but the app minimizes itself after 5 seconds. This Splash Screen call happens after the main activity was already started.
My SplashActivity looks like this:
public class SplashActivity extends AppCompatActivity {
private ImageView logo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
logo = findViewById(R.id.logo);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
finish();
}
}, 5000);
Animation anim = AnimationUtils.loadAnimation(this, R.anim.viewanimation);
logo.startAnimation(anim);
}
}
And I want to call it in my GameLoop with the following call:
Intent i = new Intent(gamePanel.getContext(), SplashActivity.class);
gamePanel.getContext().startActivity(i);
The SplashScreen opens and the animation works, but then the MainActivity should be present again (not started new).
Try this one Splash.java
public class Splash extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
Thread myThread=new Thread(){
#Override
public void run() {
try {
Animation anim = AnimationUtils.loadAnimation(this, R.anim.viewanimation);
logo.startAnimation(anim);
//sleep time after animation
sleep(3000);
//to end the splash screen and call next activity with Intent
finish();
Intent intent = new Intent(Splash.this,MainActivity.class);
startActivity(intent);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
myThread.start();
}
}
In your manifest file, Splash.java will be your first activity to launch,
<activity android:name=".Splash">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Hope it helps.
using finishActivity() instead of finish().
finish() Call this when your activity is done and should be closed. The ActivityResult is propagated back to whoever launched you via onActivityResult().
finishActivity(int requestCode) is used to finish another activity that you had previously started with startActivityForResult(Intent, int)
I have an app with two activities: MainActivity, which contains a URL entry field where the user can enter a YouTube video URL and press a submit button, to start the second activity, VideoActivity, which displays some information about this video (fetched from another web server).
The app also has a feature to receive intent via the Youtube application. When user presses the share button within the Youtube app, my app appears in the share list. Upon pressing share from the Youtube app, MainActivity should be brought to the front, and the URL should be posted within the MainActivity's URL field.
However, this only happens correctly on the first share. If the app is in the background when user shares from Youtube app, they are taken to whatever the last visible activity was, whether it is MainActivity or VideoActivity, (and even if it is MainActivity, the URL is not posted into the URL field, but the field is left in whatever state it was in when the app was last visible).
Here is my current AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.youcmt.youdmcapp">
<uses-permission android:name="android.permission.INTERNET"/>
<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"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain"/>
</intent-filter>
</activity>
<activity
android:name=".VideoActivity"
android:parentActivityName=".MainActivity"/>
<service
android:name=".FetchVideoService"
android:exported="false"/>
</application>
</manifest>
Here is my MainActivity.java code:
public class MainActivity extends AppCompatActivity {
private ResponseReceiver mReceiver;
private EditText mUrlEditText;
private Button mSearchButton;
private ProgressBar mProgressBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
super.onCreate(savedInstanceState);
mUrlEditText = findViewById(R.id.url_search_et);
Intent intent = getIntent();
if (intent.getType()!=null &&
intent.getType().equals("text/plain")) {
Bundle extras = getIntent().getExtras();
String value = extras.getString(Intent.EXTRA_TEXT);
if(value!=null)
{
mUrlEditText.setText(value);
}
}
mProgressBar = findViewById(R.id.progress_bar);
mSearchButton = findViewById(R.id.search_button);
mSearchButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
try {
askForVideo(mUrlEditText.getText().toString());
mSearchButton.setVisibility(View.INVISIBLE);
mProgressBar.setVisibility(View.VISIBLE);
} catch (Exception e) {
mUrlEditText.setText("");
mUrlEditText.setHint(e.getMessage());
e.printStackTrace();
}
}
});
}
#Override
protected void onResume() {
super.onResume();
//register the ResponseReceiver
mReceiver = new ResponseReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(FETCH_VIDEO_INFO);
registerReceiver(mReceiver, intentFilter);
}
private void askForVideo (String url) throws Exception {
try {
Intent intent = FetchVideoService.newIntent(this, url);
startService(intent);
} catch (Exception e) {
mUrlEditText.setText(e.getMessage());
}
}
public class ResponseReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
int status = intent.getIntExtra(EXTRA_VIDEO_STATUS, FAIL);
mProgressBar.setVisibility(View.INVISIBLE);
mSearchButton.setVisibility(View.VISIBLE);
if(status==FAIL)
{
mUrlEditText.setText("");
mUrlEditText.setHint("Error retrieving video!");
}
else if(status==SUCCESS) {
Video video = intent.getParcelableExtra(EXTRA_VIDEO);
Intent videoActivityIntent =
VideoActivity.newIntent(getApplicationContext(), video);
startActivity(videoActivityIntent);
}
}
}
#Override
protected void onPause() {
unregisterReceiver(mReceiver);
super.onPause();
}
}
I do not think any of the other files will be useful in understanding the problem. Although this seems like something many app creators should have to deal with, I can find no answers to this problem. Please comment if you feel I should add any additional information and thank you in advance for any help!
Update: testing demonstrates that after the first use of "Share" from YouTube (and considering app remains in the background), the MainActivity no longer receives any new intent on further shares. However, my app is still brought to the foreground somehow. This is very confusing to me.
When you share from another app, your MainActivity is brought to the front and onNewIntent() is called on it. You don't override onNewIntent() so you never see the share Intent.
I'm trying for well over 10 hours now and I can't seem to think about anyhting else. I tried every possible example on the internet, but to no avail.
I have NotificationMonitor class extending NotificationListenerService and I wanted to send message from this service to main activity(and possible other activities and services in the future) using Intent mechanism. I post code below:
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.testpackage.test">
<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>
<service android:name=".NotificationMonitor"
android:label="#string/app_name"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
</service>
</application>
</manifest>
MainActivity.java
public class MainActivity extends Activity {
private TextView txtView;
private NotificationReceiver nReceiver;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtView = (TextView) findViewById(R.id.textView);
//create receiver
nReceiver = new NotificationReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction("com.testpackage.test.NOTIFICATION_MONITOR");
registerReceiver(nReceiver,filter);
}
#Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(nReceiver);
}
public void buttonClicked(View v){
if(v.getId() == R.id.btnTestBroadcast){
//send test intent without category
Log.d("ActivityMain","Button clicked");
Intent i = new Intent("com.testpackage.test.NOTIFICATION_MONITOR");
sendBroadcast(i);
}
}
class NotificationReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
Log.d("ActivityMain","Intent received: "+intent.getAction()+" has extra: "+intent.hasExtra("info"));
if (intent.hasCategory("com.testpackage.test.TEST_CATEGORY")) {
if (intent.hasExtra("info")) {
txtView.setText(intent.getStringExtra("info"));
}
}
}
}
}
NotificationMonitor.java
public class NotificationMonitor extends NotificationListenerService {
private NotificationMonitorReceiver receiver;
#Override
public void onCreate() {
super.onCreate();
receiver = new NotificationMonitorReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction("com.testpackage.test.NOTIFICATION_MONITOR");
registerReceiver(receiver,filter);
}
#Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(receiver);
}
#Override
public void onNotificationPosted(StatusBarNotification sbn) {
//do something
sendInfo("notification posted");
}
#Override
public void onNotificationRemoved(StatusBarNotification sbn) {
//do something
sendInfo("notification removed");
}
#Override
public void onListenerConnected() {
//service created and listener connected
Log.d("NM","Listener connected!");
sendInfo("listener connected");
}
private void sendInfo(String info) {
Log.d("NM", "sendInfo called!");
Intent i = new Intent("com.testpackage.test.NOTIFICATION_MONITOR");
i.addCategory("com.testpackage.test.TEST_CATEGORY");
i.putExtra("info", info);
sendBroadcast(i);
}
class NotificationMonitorReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
//no categories intents get replied
Log.d("NM","Intent received: "+intent.getAction()+" has categories: "+(intent.getCategories()!=null));
if (intent.getCategories() == null) {
Intent i = new Intent("com.testpackage.test.NOTIFICATION_MONITOR");
i.addCategory("com.testpackage.test.TEST_CATEGORY");
sendBroadcast(i);
}
}
}
}
After running this app in debug mode of course I need to re-enable notification permissions, so when I do I see in logcat:
10-10 16:22:46.428 7330-7381/com.testpackage.test D/NM: Listener connected!
10-10 16:22:46.428 7330-7381/com.testpackage.test D/NM: sendInfo called!
well, I should receive broadcast in my application, shouldn't I?
After I click button:
10-10 16:22:57.607 7330-7330/com.testpackage.test D/ActivityMain: Button clicked
10-10 16:22:57.612 7330-7330/com.testpackage.test D/ActivityMain: Intent received: com.testpackage.test.NOTIFICATION_MONITOR has extra: false
10-10 16:22:57.619 7330-7330/com.testpackage.test D/NM: Intent received: com.testpackage.test.NOTIFICATION_MONITOR has categories: false
so the Intent is properly created and send from main activity, received back by the same activity and NotificationListenerService, has no categories so should get replied but nothing happens like when sendInfo method is called.
Please help, I have no other ideas about what might be wrong.
edit: I tested with regular services and of course Broadcasts are working just fine. Is there by chance any possibility that you just can't sendBroadcast from this particular extended service class?
Oficially, I'm a moron. Answer is: I didn't set up Category filter in IntentFilter and this is why I received zero properly sent intents from my class. So, long story short, to "fix" this mistake all one needs to do is add:
filter.addCategory("com.testpackage.test.TEST_CATEGORY");
and that's it. Thank you for your attention.
I created one app with two activities when I run app It opens first launcher activity In this activity I added textViewon clicking this textViewsecond activity is opened,
At the second activity again I added one textView on clicking that textView I expected to launch my first activity(Launcer activity) but this doesn't happens? Why? My Manifest file is as follows
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.adbs.abs.dhanagarmaza" >
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme" >
<activity
android:name=".LoginRegi"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Register"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar" >
<intent-filter>
<action android:name="com.adbs.abs.REGISTER" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
My LoginRegi(Second activity java file 'DEFAULT activity')
protected void onCreate(Bundle registerBundle) {
super.onCreate(registerBundle);
setContentView(R.layout.register);
// If user wants to login then on click "Login Me" textView open activity(LoginRegi.xml)
loginMe = (TextView)findViewById(R.id.tvLoginMe);
loginMe.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent openLoginRegi = new Intent("android.intent.action.MAIN");
startActivity(openLoginRegi);
}
});
}
and LoginRegi.java (First activity 'LAUNCHER activity')
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_regi);
// On click signup textView => Open activity (register.xml)
signUp = (TextView)findViewById(R.id.tvSignUp);
signUp.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent openRegister = new Intent("com.adbs.abs.REGISTER");
startActivity(openRegister);
}
});
}
Activity 1:-
public class MainActivity extends Activity {
TextView tvOne;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvOne=(TextView)findViewById(R.id.tvOne);
tvOne.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent(getApplicationContext(),Main2Activity.class);
startActivity(intent);
}
});
}
}
Activity 2:-
public class Main2Activity extends Activity {
TextView tvTwo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
tvTwo=(TextView)findViewById(R.id.tvTwo);
tvTwo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent(getApplicationContext(),MainActivity.class);
startActivity(intent);
}
});
}
}
Intent i =new Intent(this,MainActivity.class);
startActivity(i);
you can post your code so we can get exact problem because ..
by finish(); you can get your prevous activity back either you can do it by Intent
My LoginRegi(Second activity java file 'DEFAULT activity')
protected void onCreate(Bundle registerBundle) {
super.onCreate(registerBundle);
setContentView(R.layout.register);
// If user wants to login then on click "Login Me" textView open activity(LoginRegi.xml)
loginMe = (TextView)findViewById(R.id.tvLoginMe);
loginMe.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent openLoginRegi = new Intent(Register.this,LoginRegi.class);
startActivity(openLoginRegi);
}
});
}
and LoginRegi.java (First activity 'LAUNCHER activity')
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_regi);
// On click signup textView => Open activity (register.xml)
signUp = (TextView)findViewById(R.id.tvSignUp);
signUp.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent openRegister = new Intent(LoginRegi.this,Register.class);
startActivity(openRegister);
}
});
}