How to destroy previous activity when you jumped to next activity - java

I have activity A, having timer thread. after 5 sec it jumps to activity B via intent. how to destroy activity A when you are on activity B so that back button don't let you go back to activity A.

You have to clear the activity backstack.
intent = new Intent(this, Activity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
StartActivity(intent);

just call finish() when you make the intent from A to B

Try this...
MainActivity.java:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Waiting(5);
}
public class Waiting {
Timer timer;
public Waiting(int seconds) {
timer = new Timer();
timer.schedule(new WaitingTask(), seconds * 1000);
}
class WaitingTask extends TimerTask {
#Override
public void run() {
System.out.println("Hi, I'm waiting here!");
Intent intent = new Intent(MainActivity.this,
MainActivity1.class);
MainActivity.this.finish();
startActivity(intent);
timer.cancel();
}
}
}
}
MainActivity1.java
public class MainActivity1 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity1);
}
}

Related

Is there a solution that access MainActivity from other Activity class?

I want to call function below.
mobileView.loadUrl("javascript:setUserId () ");
And mobileView is in MainActivity.
public class MainActivity extends AppCompatActivity {
private WebView mobileView;
}
I want to call function above after SecondActivity is finished.
public class SecondActivity extends AppCompatActivity {
...
private void getUserId () {
...
finish();
}
}
And SecondActivity starts from AReceiver.
public class AReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
...
Intent i = new Intent(context, SecondActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
...
context.startActivity(i);
}
}
And AReceiver is called by below code...
class MainActivity {
...
private void userId () {
...
intent = new Intent(MainActivity.this, AReceiver.class);
pendingIntent = PendingIntent.getBroadcast(
MainActivity.this,
alarmID,
intent,
PendingIntent.FLAG_UPDATE_CURRENT
);
alarmManager.set(
AlarmManager.RTC_WAKEUP,
mCalendar.getTimeInMillis(),
pendingIntent
);
...
}
...
}
But I don't know how to access mobileView from SecondActivity.
Is there any solution?
What I've tried...
change variable from
private webView mobileView
to
public webView mobileView
2.
call from SecondActivity Using
MainActivity.mobileView
It doesn't work for me.
You can try This option
Intent
..............
You opened the new activity from another activity with startActivityForResult. In that case you can just call the finishActivity() function from your code and it'll take you back to the previous activity.
it work only if you start the second activity from main activity
I solved this problem using local broad cast manager! :D
Thank you #Shripad Jadhav who help this answer.
Below description is solution! Hope it helpes somebody. :D
MainActivity.java
public class MainActivity extends AppCompatActivity {
private WebView wv;
#Override
protected void onCreate(Bundle savedInstanceState) {
...
LocalBroadcastManager.getInstance(this)
.registerReceiver(mBroadcastReceiver, new IntentFilter("webview-filter"));
...
}
...
private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
mv.loadUrl("javascript: loadMainData ()");
}
};
#Override
protected void onDestroy() {
LocalBroadcastManager.getInstance(this).unregisterReceiver(mBroadcastReceiver);
super.onDestroy();
}
}
SecondActivity.java
public class SecondActivity extends AppCompatActivity {
private void someFunction () {
sendMessage ();
}
...
private void sendMessage() {
Log.d("sender", "Broadcasting message");
Intent intent = new Intent("webview-filter");
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
...
}

load 100% data using services while displaying splash screen

I have an Android App, which shows a "Splash Screen" for 3 seconds. After that, the MainActivity gets loaded.
Unfortunately the MainActivity takes additional ~4 seconds to load. On first startup even longer. However when the App is loaded, everything runs smooth.
I am using Async-Task for this, but desired result is not obtained and sometimes app crashes. Can someone help me please?
SplashScreen.java
public class SplashScreen extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
new Fetchdata().execute();
}
private class Fetchdata extends AsyncTask {
#Override
protected Void doInBackground(Void... voids) {
Intent intent = new Intent(SplashScreen.this,FetchApiService.class);
startService(intent);
for (int i = 0; i < 3; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Thread.interrupted();
}
}
return null;
}
#Override
protected void onPostExecute(Void result){
Intent i = new Intent(SplashScreen.this, MainActivity.class);
startActivity(i);
finish();
}
}
#Override
public void onDestroy(){
super.onDestroy();
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Some heavy processing
//starting services
Intent intent = new Intent(this,FetchApiService.class);
startService(intent);
}
}

How to end a handler and change activity with a button?

Hello I am a college student with a background of html and JavaScript making this app as a side project for work. I would like help with ending a count down timer early via a button press while also taking the user to another activity. I have used two different guides to assemble my code and I have the timer working. I have a limited knowledge of java so any help would be welcomed, here is my code:
public class Main2Activity extends AppCompatActivity {
Button button1;
int flag=0;
// page should show up for 2 seconds then take you to the main page, but I want a button there to stop the count down and take the user to a different page That I will use as a credits page
private static int TIME_OUT = 2000; //Time to launch the another activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent i = new Intent(Main2Activity.this, MainActivity.class); //Main2Activity is the Welcome page and MainActivity is the home page
startActivity(i);
finish();
}
}, TIME_OUT);
button1 = (Button) findViewById(R.id.credit);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
flag = 1;
Handler.removeCallbacksAndMessages(null);
Intent intent = new Intent(Main2Activity.this, credit.class);
startActivity(intent);
}
});
}
}
You should declare a global Handler and use it everywhere. Don't use final in Handler
Handler handler;
private static int TIME_OUT = 2000; //Time to launch the another activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
Intent i = new Intent(Main2Activity.this, MainActivity.class); //Main2Activity is the Welcome page and MainActivity is the home page
startActivity(i);
finish();
}
}, TIME_OUT);
button1 = (Button) findViewById(R.id.credit);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
flag = 1;
handler.removeCallbacksAndMessages(null);
Intent intent = new Intent(Main2Activity.this, credit.class);
startActivity(intent);
}
});
}
You can use sendMessage+removeMessage to CANCEL a post but you cant execute it immediately.
Handler h = new Handler() {
public void handleMessage(int what){
Intent i = new Intent(Main2Activity.this, MainActivity.class);
startActivity(i);
finish();
}
}
h.sendEmptyMessageDelayed(111, TIME_OUT);
//On your button Listener
h.removeMessages(111);
This way the message will be canceled and not fired in the timeout.

Timer() function not working when going to another activity

In my application i want to close it after 5 seconds using Timer() function.It works when i am in MainActivity but when i go to another activity then the application do not close.Now how to run this Timer() function in background if i switch activity.What to do in this case?
Timer timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
finish();
}
}, 5000); // Application will be closed after 5 seconds
You achieve this using broadcast receiver. in your activity which you want to finish you need to create broadcast receiver.
public class TestActivity extends Activity {
public static String intent_filter_finish = "com.test.finish";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
registerReceiver(finishReceiver,
new IntentFilter(intent_filter_finish));
}
#Override
protected void onDestroy() {
unregisterReceiver(finishReceiver);
super.onDestroy();
}
BroadcastReceiver finishReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
finish();
}
};
}
now in your second activity you need to send broadcast after 5 second e.g.
public class SecondActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
sendBroadcast(new Intent(TestActivity.intent_filter_finish));
}
}, 5000);
}
}
or other possible way is directly use postDelayed() method in your test activity e.g.
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
finish();
}
}, 5000);

How to run startActivity from outside of the main activity

I have a MainActivity class which has a NotificationsListener (seperate class). When a notification appears, it calls my gethtml class. They both extend Activity but the startActivity in my gethtml class doesn't work... (If I copy and test this code in my MainActivity, it works fine)... Anyone have an idea why it isn't working?
This is the main class:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IntentFilter filter = new IntentFilter();
filter.addAction("com.kpbird.nlsexample.NOTIFICATION_LISTENER_EXAMPLE");
startService(new Intent(MainActivity.this, NLService.class));
Intent intent=new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");
startActivity(intent);
}
#Override
protected void onDestroy() {
super.onDestroy();
}
}
This is the notification listener:
public class NLService extends NotificationListenerService {
#Override
public void onCreate() {
super.onCreate();
IntentFilter filter = new IntentFilter();
filter.addAction("com.kpbird.nlsexample.NOTIFICATION_LISTENER_SERVICE_EXAMPLE");
}
#Override
public void onDestroy() {
super.onDestroy();
}
#Override
public void onNotificationPosted(StatusBarNotification sbn) {
new Thread(new Runnable() {
public void run(){
Looper.prepare();
int cangethtml = 1;
try{
if(cangethtml==1){
cangethtml = 0; //only runs once
new html();
}
}finally{Looper.loop();}
};
}).start();
}
#Override
public void onNotificationRemoved(StatusBarNotification sbn) {}
}
This is the final class which doesn't open the website through the startActivity:
public class html extends Activity{
public html() {
try {
Intent i2 = new Intent("android.intent.action.MAIN");
i2.setComponent(ComponentName.unflattenFromString("com.android.chrome/com.android.chrome.Main"));
i2.addCategory("android.intent.category.LAUNCHER");
i2.setData(Uri.parse("https://wwww.google.com"));
startActivity(i2);
} finally{}
}
}
With reference to the developer's forum:
You should write
Context.startActivity(i2);
instead of
startActivity(i2);
Plus, make sure you don't forget to have a corresponding <activity> declaration in your package's AndroidManifest.xml.
For instance:
Check in AndroidManifest.xml for all the classes defined by you. You have:
<activity android:name="packageName.className"/>
If your html class is not defined in AndroidManifest.xml, you can not access the methods and layouts defined in it.
You need the context of the Activity. startActivity will be called in the context of Activity.

Categories

Resources